【发布时间】:2017-04-04 23:34:30
【问题描述】:
很多人都发布过这个错误,但我想不通。
当用户点击保存时,我希望“Campaign”条目是数据库的名称,然后我希望将“Workers”条目添加到名为 (workers) 的表中。
我认为问题在于,当我尝试使用 for loop 和 c.execute("insert into workersNames (workers)", element) 将信息添加到表格时,这可能只是问题之一。
import tkinter as tk
from tkinter import *
from tkinter.ttk import *
import sqlite3
class Research(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()
def initUI(self):
self.parent.title("")
self.style = Style()
self.style.theme_use("default")
self.pack(fill=BOTH, expand=True)
# CAMPAIGN
campFrame = Frame(self)
campFrame.pack(fill=X)
campLbl = Label(campFrame, text="Campaign: ", width=8)
campLbl.pack(side=LEFT, padx=5, pady=5)
campEntry = Entry(campFrame)
campEntry.pack(fill=X, padx=5, expand=True)
self.campStr = tk.StringVar()
# CITY
cityFrame = Frame(self)
cityFrame.pack(fill=X)
cityLbl = Label(cityFrame, text="City: ", width=8)
cityLbl.pack(side=LEFT, padx=5, pady=8)
cityEntry = Entry(cityFrame)
cityEntry.pack(fill=X, padx=5, expand=True)
self.cityStr = tk.StringVar()
# PROVINCE
provFrame = Frame(self)
provFrame.pack(fill=X)
provLbl = Label(provFrame, text="Province: ", width=8)
provLbl.pack(side=LEFT, padx=5, pady=8)
provEntry = Entry(provFrame)
provEntry.pack(fill=X, padx=5, expand=True)
self.provStr = tk.StringVar()
# WORKERS
workersFrame = Frame(self)
workersFrame.pack(fill=X)
workersLbl = Label(workersFrame, text="Workers: ", width=8)
workersLbl.pack(fill=X, padx=5, expand=True)
workersEntry1 = Entry(workersFrame)
workersEntry1.pack(fill=X, padx=5, expand=True)
self.workersStr1 = tk.StringVar()
workersEntry2 = Entry(workersFrame)
workersEntry2.pack(fill=X, padx=5, expand=True)
self.workersStr2 = tk.StringVar()
workersEntry3 = Entry(workersFrame)
workersEntry3.pack(fill=X, padx=5, expand=True)
self.workersStr3 = tk.StringVar()
# SAVE
saveFrame = Frame(self, relief=RAISED, borderwidth=1)
saveFrame.pack(fill=BOTH, expand=True)
saveButton = Button(self, text="Save", command = self.SaveDetails)
saveButton.pack(side=RIGHT, padx=5, pady=5)
def SaveDetails(self):
# Create Database
conn = sqlite3.connect(self.campStr.get() + ".db")
c = conn.cursor()
# Create Table - workersNames
c.execute('''create table if not exists workersNames(
workers text)''')
conn.commit()
# Add data to table
names = [self.workersStr1.get(), self.workersStr2.get(), self.workersStr3.get()]
for element in names:
c.execute("insert into workersNames (workers)", element)
conn.commit()
c.close()
conn.close()
def main():
root = Tk()
root.geometry("300x300+300+300")
app = Research(root)
root.mainloop()
if __name__ == '__main__':
main()
【问题讨论】:
-
您的
insert声明没有说明要插入的内容。请参阅insert statement docs。 -
@user2357112 我添加了
values所以它看起来像这样` c.execute("insert into workersNames values (workers)", element)` 但现在错误是sqlite3.OperationalError: no such column: workers但我认为列是在我创建表的行中创建的?我使用link 作为我的代码的模型。