【问题标题】:Generete random ID SQlite3 using Tkinter使用 Tkinter 生成随机 ID SQlite3
【发布时间】:2020-12-08 05:01:12
【问题描述】:

我正在尝试使用 TKinter 库构建库 GUI。我有一个 TreeView 显示数据库中的数据,但我希望自动生成 ID 列。创建表的代码被注释了,因为我已经运行过一次并且我确实创建了表。 The error I am getting is this.<<<<<<<<

def Add_New():
    #Create Second Window
    add_win=Toplevel()
    add_win.geometry("500x310")
    add_win.title("Add a new item")
    #Create a database or connect to one
    conn=sqlite3.connect('warehouse.db')
    #Create cursor
    c=conn.cursor()
    #Create Table ONLY ONCE after that comment it.
    #c.execute("""CREATE TABLE inventory(
    #   id_no integer primary key AUTOINCREMENT,
    #   customer_name text,
    #   part_no integer,
    #   tool_no text,
    #   descr_item_produced text,
    #   customer_prod_code text,
    #   location_item text,
    #   bar_code text,
    #   notes_comments text
    #   )""")
    #Commit Changes
    conn.commit()
    #Create Submit Function for New Item
    def submit():
        conn=sqlite3.connect('warehouse.db')
        #Create cursor
        c=conn.cursor() 
        #Insert Into Table
        c.execute("INSERT INTO inventory VALUES(: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(),
                })
        #Commit Changes
        conn.commit()
        #Close Connection with db
        conn.close()

【问题讨论】:

标签: python sqlite tkinter treeview auto-increment


【解决方案1】:

您可以在 INSERT 语句中将 NULL 作为自动增量字段 id_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(),
    })

【讨论】:

    【解决方案2】:

    虽然您为 id_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_no()。 您应该在执行 sql 命令时包含id_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()
    

    希望能回答你的问题。

    【讨论】:

      猜你喜欢
      • 2018-04-02
      • 2014-05-26
      • 2013-01-21
      • 1970-01-01
      • 2020-11-03
      • 1970-01-01
      • 2021-02-07
      • 2020-11-03
      • 1970-01-01
      相关资源
      最近更新 更多