将数据从Tkinter插入到Sqlite3 - sqlite3.OperationalError:near")":语法错误

时间:2017-04-04 23:34:31

标签: python tkinter sqlite

很多人都发布了这个错误,但我无法弄清楚。

这是我的Tkinter GUI的图片:

当用户点击“保存”时,我想要'广告系列' entry是数据库的名称,然后我想要' Workers'要添加到名为(workers)的表中的条目。

我认为问题是当我尝试使用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()

1 个答案:

答案 0 :(得分:2)

要将worker插入数据库,请使用:

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

我意识到你有很多self.xx = tk.StringVar()未连接到任何tkinter小部件。因此,当您致电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

我希望它有所帮助。