Python 使用Tkinter的Generete随机ID SQlite3
我正在尝试使用TKinter库构建一个库GUI。我有一个树状视图,显示数据库中的数据,但我希望自动生成ID列。创建表的代码被注释了,因为我已经运行了一次,并且确实创建了表。Python 使用Tkinter的Generete随机ID SQlite3,python,sqlite,tkinter,treeview,auto-increment,Python,Sqlite,Tkinter,Treeview,Auto Increment,我正在尝试使用TKinter库构建一个库GUI。我有一个树状视图,显示数据库中的数据,但我希望自动生成ID列。创建表的代码被注释了,因为我已经运行了一次,并且确实创建了表。 我得到的错误是这样的。虽然您为id\u no提供了AUTOINCREMENT,但我宁愿按照以下方法执行,并从约束中删除AUTOINCREMENT: def id_no(): mydb = mysql.connector.connect(host='localhost',
我得到的错误是这样的。虽然您为
id\u no
提供了AUTOINCREMENT
,但我宁愿按照以下方法执行,并从约束中删除AUTOINCREMENT
:
def id_no():
mydb = mysql.connector.connect(host='localhost',
user='root',
passwd='root',
database='warehouse')
mycursor = mydb.cursor()
sql = "select id_no from warehouse"
mycursor.execute(sql)
rows = mycursor.fetchall()
for i in rows :
id_no = i
try:
return id_no[0]+1
except:
return int(1)
mydb.close()
并在submit()
函数中传递函数id\u no()
。
您应该在执行sql命令时包含id\u no
列以避免错误
还要在从表中删除行后自动设置正确的数字,
使用以下sql命令
sql1 = "SET @rank:=0;"
sql2 = "update patients set id_no=@rank :=@rank+1;"
mydb = mysql.connector.connect(host='localhost',
user='root',
passwd='root',
database='warehouse')
mycursor = mydb.cursor()
mycursor.execute(sql1)
mycursor.execute(sql2)
mydb.commit()
mydb.close()
希望这能回答您的问题。您可以在INSERT语句中将
NULL
作为自动增量字段id\u no
的内容传递:
c.execute("INSERT INTO inventory VALUES(NULL,:c_name,:p_no,:t_no,:d_item,:c_code,:l_item,:b_code,:n_comm)",
{
'c_name' : c_name.get(),
'p_no' : p_no.get(),
't_no':t_no.get(),
'd_item':d_item.get(),
'c_code':c_code.get(),
'l_item':l_item.get(),
'b_code':b_code.get(),
'n_comm':n_comm.get(),
})
请提供一份报告。此外,除非绝对必要,否则请不要以图像形式共享信息。见:。
c.execute("INSERT INTO inventory VALUES(NULL,:c_name,:p_no,:t_no,:d_item,:c_code,:l_item,:b_code,:n_comm)",
{
'c_name' : c_name.get(),
'p_no' : p_no.get(),
't_no':t_no.get(),
'd_item':d_item.get(),
'c_code':c_code.get(),
'l_item':l_item.get(),
'b_code':b_code.get(),
'n_comm':n_comm.get(),
})