sqlite3.InterfaceError: Error binding parameter 1 – probably unsupported type

Question:

I am having this annoying error that I could not solve.. here is my function

def savePicture(pic):
try:
    connection=sqlite3.connect('/home/faris/Desktop/site/site.db')
    db=connection.cursor()
    print type(pic.user.profile_picture)
    db.execute('INSERT INTO pictures (picture_id, caption, created_time, picture_url, link, username,full_name,profile_picture) VALUES (?,?,?,?,?,?,?,?)',
        [
        pic.id,
        pic.caption,
        pic.created_time,
        pic.get_standard_resolution_url(),
        pic.link,
        pic.user.username,
        pic.user.full_name,
        pic.user.profile_picture
        ])
    connection.commit()
    connection.close()
except sqlite3.IntegrityError:
    print 'pic already exist'

And here is my Table (Sqlite 😀 )

-- Describe PICTURES
CREATE TABLE "pictures" (
"picture_id" INTEGER PRIMARY KEY,
"caption" TEXT,
"created_time" TEXT,
"picture_url" TEXT,
"link" TEXT,
"username" TEXT,
"full_name" TEXT,
"profile_picture" TEXT
)

And this is the error I am having,

<type 'str'>
Traceback (most recent call last):
File "/home/faris/Desktop/site/cron/pictures.py", line 15, in <module>
savePicture(picture)
File "/home/faris/Desktop/site/db.py", line 36, in savePicture
pic.user.profile_picture
sqlite3.InterfaceError: Error binding parameter 1 - probably unsupported type.

As you see I have printed the type of the “pic.user.profile_picture” and it returned str
I have also tried using these two functions ( unicode and str ) just to make sure that it is returning a string with no luck..

Any ideas? cheers 😀

Asked By: Faris

||

Answers:

Ok, That is stupid lol

    pic.caption,
    pic.created_time,

are not TEXT type..but the error msg is saying the problem
from pic.user.profile_picture.
thus, if you have this error just check all the parameters 🙂

Read the comment below 🙂

Answered By: Faris

The easiest way to solve that issue is- convert all the dataframe columns into str, and apply to_sql method.
df = df.applymap(str)
otherwise, you can change the data types of each column that are compatible with SQLite datatypes before saving the dataframe into SQLite table. dtype parameter of to_sql method would be useful to convert the datatypes of the columns while inserting the data into SQL table.

Answered By: Guru Bhandari

The error message

Error binding parameter 1 – probably unsupported type

is reporting that the value at position 1* in the sequence of values passed to cursor.execute is an instance of a type not supported by Python’s sqlite3 module.

Python’s sqlite3 module supports passing None, int, float, str, bytes by default, and also knows how to adapt datetime.date and datetime.datetime instances.

To fix the error, convert the value at the reported position to a supported type.

It’s possible to simulate storing unsupported types in the database by registering adapters and converters to convert objects of that type to and from one of the supported types**. This is the mechanism used to handle date and datetime instances. Imagine that we have a Fruit class that we want to be able to store and retrieve in a database.

class Fruit:
    def __init__(self, name):
        self.name = name

    def __repr__(self):
        return f'Fruit({self.name!r})'

We create adapter and converter functions, and register them with sqlite:

def adapt_fruit(fruit):
    # We must return a supported type.
    return fruit.name

def convert_fruit(value):
    # value will always be bytes.
    return Fruit(value.decode('utf-8'))

sqlite3.register_adapter(Fruit, adapt_fruit)

# The first argument is the type of column that will store "fruits".
sqlite3.register_converter('fruit', convert_fruit)

We can pass detect_types to the connection and create a table with column of type fruit:

apple = Fruit('Apple')
banana = Fruit('Banana')

with sqlite3.connect('so12952546.db', detect_types=sqlite3.PARSE_DECLTYPES) as conn:
    cur = conn.cursor()
    cur.execute("""DROP TABLE IF EXISTS tbl""")
    # Create a column with our "fruit" type.
    cur.execute("""CREATE TABLE tbl (name fruit)""")
    # Pass Fruit instances as values.
    cur.executemany("""INSERT INTO tbl (name) VALUES (?)""", [(apple,), (banana,)])
    conn.commit()
    cur.execute("""SELECT name FROM tbl""")
    # Fruit instances are returned.
    for row in cur:
        print(row)

Output:

(Fruit('Apple'),)
(Fruit('Banana'),)

The column will have type fruit in Sqlite, but it will be associated with a supported storage class: text, in this case.

sqlite> .schema tbl
CREATE TABLE tbl (name fruit);
sqlite> SELECT name, typeof(name) FROM tbl;
Apple|text
Banana|text

* The position is computed using zero-based counting, so position one is the second value

** This example doesn’t cover all the options – read the module documentation for all the details.

Answered By: snakecharmerb
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.