Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 8th, 2012  |  syntax: None  |  size: 0.66 KB  |  hits: 8  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Inserting a variable to the database using sqlite in Python
  2. import sqlite3
  3. db = sqlite3.connect("dbse.sqlite")
  4. cursor= db.cursor()
  5. cursor.execute("CREATE TABLE Myt (Test TEXT)")
  6. variable = ('aaa')
  7. cursor.execute('INSERT INTO Myt VALUES (?)' , variable)
  8. db.commit()
  9.        
  10. cursor.execute('INSERT INTO Myt VALUES (?)' , variable)
  11. sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 3 supplied.
  12.        
  13. variable = ('aaa',) # Notice the comma
  14.        
  15. >>> tuple('aaa')
  16. ('a', 'a', 'a')
  17. >>> ('aaa',)
  18. ('aaa',)
  19.        
  20. >>> len(variable)
  21. 3
  22. >>> list(variable)
  23. ['a', 'a', 'a']
  24.        
  25. cursor.execute('INSERT INTO Myt VALUES (?)', (variable,))