2017-04-04 96 views
-1

很多人發佈了關於此錯誤的消息,但是我無法弄清楚。將數據從Tkinter插入到Sqlite3 - sqlite3.OperationalError:near「)」:語法錯誤

這是我的Tkinter GUI的一個畫面:

當用戶點擊保存,我想「廣告系列」條目是數據庫的名稱,然後我想「工人」的條目被添加到名爲(工人)的表中。

我認爲問題是當我嘗試將信息添加到for loopc.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() 
+0

你的'insert'語句沒有說什麼要插入。請參閱[插入語句文檔](https://sqlite.org/lang_insert.html)。 – user2357112

+0

@ 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

回答

2

要插入人員納入數據庫,請使用:

c.execute("insert into workersNames (workers) values (?)", (element,)) 

我意識到你有很多的未連接到任何Tkinter的部件self.xx = tk.StringVar()秒。因此,當您撥打sef.xx.get()時,您會收到一個空字符串。我刪除了tk.StringVar()以使代碼縮短以供測試,並在tkinter條目上使用get()方法來獲取其內容。

因此:

names = [self.workersStr1.get(), self.workersStr2.get(), self.workersStr3.get()] 

變成了:

names = [self.workersEntry1.get(), self.workersEntry2.get(), self.workersEntry3.get()] 

通知我改變了所有的條目到類使用self關鍵字屬性。

下面是完整的代碼,你可以通過和看到的變化:

#import tkinter as tk 
#from tkinter import * 
#from tkinter.ttk import * 
#import sqlite3 

import Tkinter as tk 
from Tkinter import * 
from 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) 

     self.campEntry = Entry(campFrame) 
     self.campEntry.pack(fill=X, padx=5, expand=True) 


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


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


     # WORKERS 
     workersFrame = Frame(self) 
     workersFrame.pack(fill=X) 

     workersLbl = Label(workersFrame, text="Workers: ", width=8) 
     workersLbl.pack(fill=X, padx=5, expand=True) 

     self.workersEntry1 = Entry(workersFrame) 
     self.workersEntry1.pack(fill=X, padx=5, expand=True) 


     self.workersEntry2 = Entry(workersFrame) 
     self.workersEntry2.pack(fill=X, padx=5, expand=True) 


     self.workersEntry3 = Entry(workersFrame) 
     self.workersEntry3.pack(fill=X, padx=5, expand=True) 


     # 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.campEntry.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.workersEntry1.get(), self.workersEntry2.get(), self.workersEntry3.get()] 

     for element in names: 
      c.execute("insert into workersNames (workers) values (?)", (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() 

這將產生以下結果:

enter image description here

enter image description here

希望它有助於。

相關問題