Reputation: 33
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
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
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:
AUTOINCREMENT
, like id INTEGER PRIMARY KEY AUTOINCREMENT,...
. Take proper read here.Upvotes: 1