使用Python

时间:2016-11-08 02:25:12

标签: python tkinter sqlite

我是一般的编程新手,我正在尝试在4x4网格的checkbutton / textbox单元上处理一个简单的数据库插入/检索gui原型。

我已成功在SQLite3中创建了一个包含32个(16个文本/ 16个checkutton)相应列的表。

现在我正在尝试将这些字段绑定到各自的列,并希望使用保存按钮创建新行

...但我一直收到这个错误:

sqlite3.OperationalError: near "checkbutton": syntax error

我正在尝试运行此代码:

conn = sqlite3.connect('mainRecs.db')
c = conn.cursor()

def data_entry():
    c.execute("INSERT INTO recsTable (rec1, rec1 checkbutton,\
    rec2, rec2 checkbutton,\
    rec3, rec3 checkbutton,\
    rec4, rec4 checkbutton,\
    rec5, rec5 checkbutton,\
    rec6, rec6 checkbutton,\
    rec7, rec7 checkbutton,\
    rec8, rec8 checkbutton,\
    rec9, rec9 checkbutton,\
    rec10, rec10 checkbutton,\
    rec11, rec11 checkbutton,\
    rec12, rec12 checkbutton,\
    rec13, rec13 checkbutton,\
    rec14, rec14 checkbutton,\
    rec15, rec15 checkbutton,\
    rec16, rec16 checkbutton) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?,\
    ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,\
    ?, ?, ?, ?, ?, ?, ?);", (strVar1, checkVar1, strVar2, checkVar2,
    strVar3, checkVar3, strVar4, checkVar4,
    strVar5, checkVar5, strVar6, checkVar6,
    strVar7, checkVar7, strVar8, checkVar8,
    strVar9, checkVar9, strVar10, checkVar10,
    strVar11, checkVar11, strVar12, checkVar12,
    strVar13, checkVar13, strVar14, checkVar14,
    strVar15, checkVar15, strVar16, checkVar16))
conn.commit()

在我的脚本中,我对16个“recs”

中的每一个都有以下代码
checkVar1 = IntVar()
chk1 = Checkbutton(root, variable=checkVar1)
chk1.grid(row=2, column=0, sticky=NE)
Label(root, text="Rec 1").grid(row=2, column=0, sticky=W)
strVar1 = StringVar()
e1 = Entry(root, textvariable=strVar1)
e1.grid(row=4, column=0)
strVar1.set("Enter Rec Label")

任何关于我所缺少的内容都会非常感激!

我几乎整天环顾四周,仍然无法绕过我所缺少的东西,或者我是否正确地表达了问题

如果您有时间查看这些代码,一些反馈会非常有用。

谢谢!

-T

1 个答案:

答案 0 :(得分:0)

如果您想要IntVar的值,则必须使用get方法(例如:checkVar1.get())这在任何地方都会被提及IntVar

SQL中也存在短语rec1 checkbutton的问题。那是非法的SQL。您需要在包含空格的列名称周围添加引号(并注意在整个表达式周围使用三引号):

def data_entry():
    c.execute('''INSERT INTO recsTable (rec1, "rec1 checkbutton",
    rec2, "rec2 checkbutton",
    ...
    ''')

但是,你确定你的列名有空格吗?这似乎非常不寻常,并且使你的桌子工作更加困难(正如这个问题所证明的那样......)