-1
很多人發佈了關於此錯誤的消息,但是我無法弄清楚。將數據從Tkinter插入到Sqlite3 - sqlite3.OperationalError:near「)」:語法錯誤
當用戶點擊保存,我想「廣告系列」條目是數據庫的名稱,然後我想「工人」的條目被添加到名爲(工人)的表中。
我認爲問題是當我嘗試將信息添加到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'語句沒有說什麼要插入。請參閱[插入語句文檔](https://sqlite.org/lang_insert.html)。 – user2357112
@ user2357112我添加了'values'看起來像這樣'\t \t \t c.execute(「insert into workersNames values(workers)」,element)'但現在錯誤是sqlite3.OperationalError:no such column:workers'但我認爲該列是在創建表的行中創建的?我使用[link](https://stackoverflow.com/questions/16120474/save-button-with-python-tkinter-sqlite)作爲我的代碼的模型。 – nasan