Python 如何将Tkinter条目放入SQLite数据库中?

Python 如何将Tkinter条目放入SQLite数据库中?,python,database,sqlite,tkinter,Python,Database,Sqlite,Tkinter,这是我数据库的代码 # 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( i

这是我数据库的代码

# 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);
""")
我想在电子邮件中存储来自TKinter python代码的条目,我将如何做到这一点

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

您必须为数据库中的每列创建3个条目。假设它们是
txt1
txt2
txt3
。以下是您将如何插入它:

def push():
使用sqlite3.connect(“password_vault.db”)作为数据库:
cursor=db.cursor()
sql_命令='插入主密码值(?,?)'#查询
values=txt1.get()、txt2.get()、txt3.get()#值的元组
execute(sql_命令,值)#执行替换值的查询
这里,
是一个占位符,稍后您将替换这些值,而不是
execute
方法中的值。漫步

这里有很多事情需要注意:

  • Id列要求具有唯一的值,因此实际要求用户输入Id或创建Id并不是最佳做法,相反,您可以使用
    AUTOINCREMENT
    ,如
    Id INTEGER主键AUTOINCREMENT,…
    。适当阅读
  • 你不应该只是把密码插入数据库,你应该先把它散列,然后再插入,否则任何人都可以通过简单的数据库查询轻松获得所有密码。是一个类似的项目,我的工作,如果你的想法寻找(它还没有完善)

您需要创建一个提交按钮,从输入字段中获取值,并将其插入数据库

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

我认为不能保证
random.randint(1101)
不会在整个数据库范围内两次返回相同的数字使用
NULL
而不是random number.import uuid id=uuid.uuid1()可以用来生成唯一的id