[英]How to give Sqlite3 table name from a variable in Python
I'm in a dilemma as to how to give my table name depending on my Combobox selections.对于如何根据我的 Combobox 选择来命名我的表名,我处于两难境地。 I am using PyQt5.
我正在使用 PyQt5。 I tried it this way but got not the thing right.
我尝试过这种方式,但没有得到正确的结果。
b = self.combo.currentText()
sql = '''CREATE TABLE IF NOT EXISTS d (id int, grade text)''')
This creates a database with b as the name instead of the choice from the combobox.这将创建一个名称为 b 的数据库,而不是 combobox 中的选项。
You have to bind the event based on combobox selection using application_cb.bind("<>", createTable) where you have to pass function name您必须使用 application_cb.bind("<>", createTable) 根据 combobox 选择绑定事件,您必须传递 function 名称
application_label = Label(cbFrameRow1Col2, text='Application', bg='gray46', fg='white', font=("calibri", 10))
application_label.grid(row=0, column=0, sticky='w', padx=10, pady=5)
application_cb = ttk.Combobox(cbFrameRow1Col2, values=application_list, width=15)
application_cb.grid(row=0, column=1, sticky='w', padx=10, pady=5)
application_cb.bind("<<ComboboxSelected>>", createdb)
And now you have to call create table function when this event is triggered现在您必须在触发此事件时调用 create table function
def createTable(event):
tablename = application_cb.get()
conn = sqlite3.connect('temp.db')
cur = conn.cursor()
cur.execute("CREATE TABLE IF NOT EXISTS %s (id INTEGER PRIMARY KEY, Name TEXT)" % (tablename))
conn.commit()
conn.close()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.