我正在编写一个小程序来简单地将文本数据和整数数据保存到SQLite3
数据库中。
我创建了test.db
使用以下代码调用的数据库:
con = sqlite3.connect('test.db')
cur = con.cursor()
cur.execute('drop table if exists data')
cur.execute('create table data(t1 TEXT, i1 INT)')
con.commit()
print('DB created')
问题是当我运行它时它告诉我该cur.execute
行需要字符串作为参数,但我已经定义text1
为带有StringVar
.
from tkinter import *
from tkinter import ttk
import sqlite3
def savedata(text1, int1):
con = sqlite3.connect('test.db')
cur = con.cursor()
cur.execute(('INSERT INTO data (t1, i1) VALUES (?,?)', (text1, int1)))
con.commit()
print('record inserted in data')
root = Tk()
root.title("Little Program To Save Data")
mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column = 0, row = 0, sticky = (N,W,E,S))
mainframe.columnconfigure(0, weight = 1)
mainframe.rowconfigure(0, weight = 1)
text1 = StringVar()
text_entry = ttk.Entry(mainframe, width = 20, textvariable=text1)
text_entry.grid(column = 2, row = 1, sticky = (N,W,E,S))
int1 = IntVar()
int_entry = ttk.Entry(mainframe, width = 20, textvariable=int1)
int_entry.grid(column = 2, row = 2, sticky = (N,W,E,S))
ttk.Button(mainframe, text = "Save!", command=savedata(text1, int1)).grid(column = 3, row = 3, sticky = (W, E))
for child in mainframe.winfo_children():
child.grid_configure(padx = 5, pady = 5)
root.mainloop()