简体   繁体   中英

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. I am using 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.

You have to bind the event based on combobox selection using application_cb.bind("<>", createTable) where you have to pass function name

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM