mardi 10 février 2015

Python SQLite 3 OperationalError: table users has 2 columns but 1 values were supplied

I am trying to use a GUI in python to enter values into a database.



from Tkinter import *
import sqlite3

master = Tk()
con = sqlite3.connect('c:/work/ex1.db')
c = con.cursor()

e = Entry(master)
e.pack()
e.focus_set()

def callback():
un = e.get()
c.execute("CREATE TABLE if not exists users(un varchar(25))")
c.execute("INSERT INTO users VALUES (?);",(un))
con.commit()
print("First user inserted")

b = Button(master, text="get", width=10, command=callback)
b.pack()

mainloop()


when I run it and press the 'get' button, I get the error



Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1470, in __call__
return self.func(*args)
File "C:\Users\****\Desktop\MyPython\entry.py", line 15, in callback
c.execute("INSERT INTO users VALUES ('?');",(un))
OperationalError: table users has 2 columns but 1 values were supplied


It says that the table has 2 columns but 1 value was given. I tried using sqlite3 to do the same thing and of course that worked.


Aucun commentaire:

Enregistrer un commentaire