
Untitled
By: a guest on
Aug 8th, 2012 | syntax:
None | size: 0.66 KB | hits: 8 | expires: Never
Inserting a variable to the database using sqlite in Python
import sqlite3
db = sqlite3.connect("dbse.sqlite")
cursor= db.cursor()
cursor.execute("CREATE TABLE Myt (Test TEXT)")
variable = ('aaa')
cursor.execute('INSERT INTO Myt VALUES (?)' , variable)
db.commit()
cursor.execute('INSERT INTO Myt VALUES (?)' , variable)
sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 3 supplied.
variable = ('aaa',) # Notice the comma
>>> tuple('aaa')
('a', 'a', 'a')
>>> ('aaa',)
('aaa',)
>>> len(variable)
3
>>> list(variable)
['a', 'a', 'a']
cursor.execute('INSERT INTO Myt VALUES (?)', (variable,))