多くの人がこのエラーについて投稿していますが、わかりません。TkinterからSqlite3へのデータの挿入 - sqlite3.OperationalError:near ")":構文エラー
ユーザーが[保存]をクリックすると、私は[キャンペーン 'エントリは、データベースの名前になりたい、そして私は「労働者のエントリーをしたいです(workers)というテーブルに追加することができます。
私はこの問題が、for loop
とc.execute("insert into workersNames (workers)", element)
という表に情報を追加しようとしていると思いますが、これは問題の1つにすぎません。
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](https://sqlite.org/lang_insert.html)を参照してください。 – user2357112
@ user2357112これは、 '\t \t \t c.execute(「workersNames値(worker)」要素に挿入します)というように' values'を追加しましたが、エラーは 'sqlite3.OperationalError:no such column:workers'ですテーブルを作成する行に列が作成されていると思いましたか?私は私のコードのモデルとして[リンク](https://stackoverflow.com/questions/16120474/save-button-with-python-tkinter-sqlite)を使用しています。 – nasan