William Pearson
William Pearson

Reputation: 33

How can I put a Tkinter entry inside an SQLite database?

Here is the code for my database

# database code
with sqlite3.connect("password_vault.db") as db:
cursor = db.cursor()
# creates a table for masterpassword if one doesn't already exist
cursor.execute("""
CREATE TABLE IF NOT EXISTS masterpassword(
id INTEGER PRIMARY KEY,
password TEXT NOT NULL,
email TEXT NOT NULL);
""")

I would like to store the entry from this TKinter python code within email, how would I do this?

# user input of email
txt3 = Entry(window, width=20)
txt3.pack()
txt3.focus()

Upvotes: 0

Views: 1094

Answers (2)

Tahil Bansal
Tahil Bansal

Reputation: 163

You need to create a submit button that gets the value from the Entry fields and insert it into the database

from tkinter import *
import sqlite3
import random
window = Tk()

width = window.winfo_screenwidth()
height = window.winfo_screenheight()
window.geometry(f"{width}x{height}")


email = StringVar()
password = StringVar()

def submit():

    con = sqlite3.connect('password_vault.db')
    cur = con.cursor()
    cur.execute(f"insert into masterpassword values ({random.randint(1, 101)},'{password.get()}','{email.get()}')")
    con.commit()
    con.close()


# database code
with sqlite3.connect("password_vault.db") as db:
    cursor = db.cursor()
    # creates a table for masterpassword if one doesn't already exist
    cursor.execute("""
    CREATE TABLE IF NOT EXISTS masterpassword(
    id INTEGER PRIMARY KEY,
    password TEXT NOT NULL,
    email TEXT NOT NULL);
    """)

# user input of email
txt3 = Entry(window, width=20,textvariable=email)
txt3.pack()
txt3.focus()

Password_field = Entry(window,textvariable=password)
Password_field.pack()

submit_button = Button(window,text='Submit',command = submit)
submit_button.pack()


window.mainloop()

Upvotes: 0

Delrius Euphoria
Delrius Euphoria

Reputation: 15098

You will have to create 3 entries each for each column in your database. Lets assume those are txt1, txt2 and txt3. Here is how you will insert it:

def push():
    with sqlite3.connect("password_vault.db") as db:
        cursor = db.cursor()
        sql_command = 'INSERT INTO masterpassword VALUES (?,?,?)' # Query
        values = txt1.get(),txt2.get(),txt3.get() # tuple of values
        cursor.execute(sql_command,values) # Execute the query substituting the values

Here, ? is a placeholder and you will later substitute the values instead of that in the execute method. Take a stroll down here

There are alot of things to note here:

  • Id column requires to have unique value, so its not best practice to actually ask the user for id or even you creating one, instead you can use AUTOINCREMENT, like id INTEGER PRIMARY KEY AUTOINCREMENT,.... Take proper read here.
  • You shouldn't just insert the password into a database, you should first hash it and then insert it, or else a simple database query by anyone can get you all the passwords easily. Here is a similar project I worked on if your looking for ideas(it is not perfected yet).

Upvotes: 1

Related Questions