【问题标题】:How can I put a Tkinter entry inside an SQLite database?如何将 Tkinter 条目放入 SQLite 数据库中?
【发布时间】:2021-05-20 15:09:23
【问题描述】:

这是我的数据库的代码

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

【问题讨论】:

    标签: python database sqlite tkinter


    【解决方案1】:

    您必须为数据库中的每一列分别创建 3 个条目。让我们假设它们是txt1txt2txt3。以下是您将如何插入它:

    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
    

    这里,? 是一个占位符,稍后您将用这些值代替 execute 方法中的值。逛逛here

    这里有很多需要注意的地方:

    • Id 列需要具有唯一值,因此实际向用户询问 id 甚至您创建 ID 并不是最佳实践,您可以使用 AUTOINCREMENT,例如 id INTEGER PRIMARY KEY AUTOINCREMENT,...。正确阅读here
    • 您不应该只是将密码插入数据库,您应该先对其进行哈希处理,然后再将其插入,否则任何人都可以通过简单的数据库查询轻松获取所有密码。如果您正在寻找想法,Here 是我从事的一个类似项目(它还没有完美)。

    【讨论】:

      【解决方案2】:

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

      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(1, 101) 在数据库范围内不会返回相同的数字两次
      • 使用NULL代替随机数。
      • import uuid id = uuid.uuid1() 可用于生成唯一id
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-01
      • 1970-01-01
      • 2013-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多