Sql statement not adding actual dat to database

41 Views Asked by At

I am trying to make a sign in window for an application with tkinter and sqlite3 but all it sends to the data base repeatedly is that the username is ".!toplevel.!entry" and password is ".!toplevel.!entry2". Please help thanks

def createlogin():
global username
global password
top= Toplevel()
top.title("SmartCards - New User")
top.iconbitmap(r"C:\Users\Ethan\Documents\Comp NEA\Icon.ico")
top.config(padx=50, pady=50, bg=BACKGROUND_COLOR)

def submit1():
    c.execute("INSERT INTO login (username, password) VALUES (?, ?)", ("username, password,"),)
    conn.commit()   
    
username = Entry(top)
username.insert(0,"Username: ")
username.grid(row=0, column=0)
username=str(username)
password = Entry(top)
password.insert(0,"Password: ")
password.grid(row=1, column=0)
password=str(password)
submit = Button(top, text="Submit", command=submit1)
submit.grid(row=2, column=0)
1

There are 1 best solutions below

0
acw1668 On

There are few issues in your code:

  • username = str(username) will set username to the name of the entry widget username. Same for password.
  • passing "username, password," as the second argument of c.execute() which raises exception because a tuple/list of two items is expected
  • global ... is not necessary

Note also that you should get the contents of the entry boxes inside submit1() instead.

Below is the modified code:

def createlogin():
    top = Toplevel()
    top.title("SmartCards - New User")
    top.iconbitmap(r"C:\Users\Ethan\Documents\Comp NEA\Icon.ico")
    top.config(padx=50, pady=50, bg=BACKGROUND_COLOR)

    def submit1():
        # get the inputs here
        user = username.get()
        passwd = password.get()
        # pass (user, passwd) as second argument
        c.execute("INSERT INTO login (username, password) VALUES (?, ?)", (user, passwd))
        conn.commit()

    username = Entry(top)
    username.insert(0,"Username: ")
    username.grid(row=0, column=0)
    password = Entry(top)
    password.insert(0,"Password: ")
    password.grid(row=1, column=0)
    submit = Button(top, text="Submit", command=submit1)
    submit.grid(row=2, column=0)