Reputation: 243
I can't seem to figure out what is wrong with my code, but I keep getting the:
error "binding parameter 0 - probably unsupported type".
Here is my code:
last = 'EBERT'
sakila = connect("sakila.db")
res = sakila.execute("SELECT first_name, last_name FROM customer WHERE last_name = ?",[(last,)])
for row in res:
print(row)
When I have it where 'EBERT
' is in the query and not set to a variable, it works fine, so I know it's a problem with the tuple syntax or something. I've tried it without the brackets, with a second variable for first_name
, with and without a separately defined cursor, and basically every method I can think of, and I've researched for hours but have gotten nowhere, so any help would be super appreciated.
Upvotes: 14
Views: 77185
Reputation: 31
I had the same error when I run this code,
cur.execute('SELECT id FROM User WHERE name = ?', (username, ))
user_id = cur.fetchone()
cur.execute('INSERT OR IGNORE INTO Course(title) VALUES (?)', (course, ))
cur.execute('SELECT id FROM Course WHERE title = ?', (course, ))
course_id = cur.fetchone()
print(user_id, course_id, role)
cur.execute('''INSERT OR REPLACE INTO Member(user_id, course_id, role)
VALUES (?, ?, ?)''', (user_id, course_id, role))
the problem was that cur.fetchone() returned a list with one element not an integer value. so you need to add the square brackets to access the element.
cur.execute('INSERT OR IGNORE INTO User(name) VALUES (?)', (username, ))
cur.execute('SELECT id FROM User WHERE name = ?', (username, ))
user_id = cur.fetchone()[0]
cur.execute('INSERT OR IGNORE INTO Course(title) VALUES (?)', (course, ))
cur.execute('SELECT id FROM Course WHERE title = ?', (course, ))
course_id = cur.fetchone()[0]
print(user_id, course_id, role)
cur.execute('''INSERT OR REPLACE INTO Member(user_id, course_id, role)
VALUES (?, ?, ?)''', (user_id, course_id, role))
Upvotes: 1
Reputation: 2625
I was getting the same error, sorted out that my data type was mismatched. I then converted it into string;
cursor.execute('''INSERT INTO employees VALUES (?);''', (str(data[0]), ))
and it worked fine. Hope this will be helpful for someone.
Upvotes: 10
Reputation: 369054
Nested lists, tuples are used for executemany
, not for execute
.
Pass a flat list (or tuple) that contians parameters.
res = sakila.execute(
"SELECT first_name, last_name FROM customer WHERE last_name = ?",
(last,))
or
res = sakila.execute(
"SELECT first_name, last_name FROM customer WHERE last_name = ?",
[last])
Upvotes: 7