Python SQLite 3 OperationalError:表用户有2列,但提供了1个值

时间:2015-02-10 17:43:35

标签: python sqlite

我正在尝试在python中使用GUI将值输入数据库。

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()

当我运行它并按下“获取”时按钮,我收到错误

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

它表示该表有2列,但给出了1个值。我尝试使用sqlite3来做同样的事情,当然这很有效。

~~~~~~~

更新

我尝试使用(un,)现在我收到此错误

Traceback (most recent call last):
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1470, in __call__
    return self.func(*args)
  File "C:\Users\Zen-Work\Desktop\MyPython\entry.py", line 16, in callback
    c.execute("INSERT INTO users VALUES ('?')",(un,))
ProgrammingError: Incorrect number of bindings supplied. The current statement u
ses 0, and there are 1 supplied.

1 个答案:

答案 0 :(得分:1)

我删除了问号上的单引号。解决了这个问题。