[英]How to insert record into SQLite3 database via python?
I am very new to python, sql and programming in general and am trying to create a function that takes the two values from the entry box than inserts them into the database and then updates the Treeview to show the updated database.我对 python、sql 和编程非常陌生,我正在尝试创建一个函数,该函数从输入框中获取两个值,然后将它们插入到数据库中,然后更新 Treeview 以显示更新的数据库。
There are 3 fields in my database id
, username
and password
.我的数据库
id
、 username
和password
有 3 个字段。 I do not know what to put for the ID either as I thought it would auto-increment but when I run the code it requires a value.我不知道该为 ID 放什么,因为我认为它会自动递增,但是当我运行代码时,它需要一个值。
Here is my attempt:这是我的尝试:
def add():
with sqlite3.connect("database.db") as conn:
c = conn.cursor()
username=entry_username.get()
password=entry_password.get()
c.execute("INSERT INTO logins VALUES (?, ?, ?)", (username, password))
rows = c.fetchall()
for row in rows:
print(row)
tree.insert("", tk.END, values=row)
c.close()
Thank you for any help.感谢您的任何帮助。
If you have declared column ID with Integer and Primary key then it will automatically auto increment using ROWID.如果您使用整数和主键声明了列 ID,那么它将使用 ROWID 自动增加。 Refer: https://www.sqlite.org/faq.html#q1
参考: https : //www.sqlite.org/faq.html#q1
CREATE TABLE login (
id INTEGER PRIMARY KEY,
username text NOT NULL,
password text NOT NULL
);
And Insert by specifying the column names.并通过指定列名插入。
INSERT INTO login (username, password) VALUES (?, ?);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.