Home > Blockchain >  how can create multiple tables in database by using tkinter
how can create multiple tables in database by using tkinter

Time:06-25

hey guys I have a problem that I have an idea for an application but it needs to create multiple tables in database and the user specify its name by using 'tkinter' entry

I had used this query but it doesn't work

cr.execute("""create table {}(text1 text,text2 text ,var1 float,var2 float
    """.format(table_name))

CodePudding user response:

Adding a sample code here for reference. Please read-through as suggested by @Alias.

from tkinter  import *
import sqlite3

def create_conn(dbf):
    conn = None
    try:
        conn = sqlite3.connect(dbf)
    except Error as e:
        print(e)
    return conn
    
def create_table(conn, tname):

    cur = conn.cursor()
    sqlstrng = "create table "  tname   "(id integer)"
    cur.execute(sqlstrng)

def start(tname):
    dbfile = r"C:\sqllite\sqlite-tools-win32-x86-3380300\testdb.db"

    # Create DB connection
    conn = create_conn(dbfile)
    create_table(conn,tname)    
    
ws = Tk()
ws.title('get text demo')
ws.geometry('210x210')

def startTabCreation():
    tname = name_Tf.get()
    start(tname)

Label(ws, text="Enter Name").pack()
name_Tf = Entry(ws)
name_Tf.pack()

Button(ws, text="Create Table", command=startTabCreation).pack()

ws.mainloop()

Refer code running demo here.

  • Related