【问题标题】:Sqlite3 Table users has 7 columns but 6 values were suppliedSqlite3 表用户有 7 列,但提供了 6 个值
【发布时间】:2020-08-27 11:28:01
【问题描述】:

请问我是python和sqlite3的新手,我试图找到注册表无法提交的可能原因,它一直给我一个错误。并且提供的值不断倒计时

c.execute("INSERT INTO users VALUES (?, ?, ?, ?, ?, ?)", (element)) sqlite3.OperationalError:表用户有 7 列,但有 6 个值 提供

   # Importing Tkinter framework
            fr
    
    om tkinter import *
        from tkinter import ttk
        # Import sqlite3
        import sqlite3
        
        
        def setup_db():
            # Open db
            global conn
            conn = sqlite3.connect('shengen.db')
            # Create a cursor
            global c
            c = conn.cursor()
        
            # Create the table if it doesn't exist
            try:
                c.execute("""CREATE TABLE if not exists users(
                    ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
                    fname TEXT NOT NULL,
                    email TEXT NOT NULL,
                    password TEXT NOT NULL,
                    cPassword TEXT NOT NULL,
                    sex INTEGER NOT NULL,
                    country TEXT NOT NULL
                    );""")
        
                conn.commit()
        
            except sqlite3.OperationalError:
                print("ERROR: Table not Created")
        
        
        def reg_submit():
            # Insert record into the db
            new_user = [fname.get(), email.get(), password.get(),
                        cPassword.get(), sex.get(), country.get()]
        
            for element in new_user:
                c.execute("INSERT INTO users VALUES (?, ?, ?, ?, ?, ?)", (element))
        
            conn.commit()
            c.close()
            conn.close()
        
        
        def register():
            root1 = Toplevel(root)
            root1.geometry("900x700")
            root1.title("Registration Page")
            root1.iconbitmap("")
        
            global fname
            global email
            global password
            global cPassword
            global sex
            global sex1
            global country
        
            fname = StringVar()
            email = StringVar()
            password = StringVar()
            cPassword = StringVar()
            sex = IntVar()
            sex1 = IntVar()
            country = StringVar()
        
            Label(root1, text="", bg="grey", height="2",
                  width="900", font=("Calibri", 14)).pack()
        
            label_0 = Label(root1, text="Registration Form",
                            font=("bold", 24))
            label_0.place(x=250, y=53)
        
            label_1 = Label(root1, text="FullName:", width=20, font=("bold", 10))
            label_1.place(x=195, y=130)
        
            entry_1 = Entry(root1, width=40, textvariable=fname)
            entry_1.place(x=320, y=130)
        
            label_2 = Label(root1, text="Email:", width=20, font=("bold", 10))
            label_2.place(x=205, y=180)
        
            entry_2 = Entry(root1, width=40, textvariable=email)
            entry_2.place(x=320, y=180)
        
            label_3 = Label(root1, text="Password:", width=23, font=("bold", 10))
            label_3.place(x=180, y=230)
        
            entry_3 = Entry(root1, width=40, textvariable=password)
            entry_3.place(x=320, y=230)
        
            Label_6 = Label(root1, text="Confirm Password:",
                            width=25, font=("bold", 10))
            Label_6.place(x=150, y=280)
        
            entry_4 = Entry(root1, width=40, textvariable=cPassword)
            entry_4.place(x=320, y=280)
        
            label_4 = Label(root1, text="Gender:", width=20, font=("bold", 10))
            label_4.place(x=200, y=330)
            Radiobutton(root1, text="Male", padx=5, textvariable=sex,
                        value=1).place(x=320, y=330)
            Radiobutton(root1, text="Female", padx=20, textvariable=sex1,
                        value=2).place(x=400, y=330)
            label_5 = Label(root1, text="Country", width=20, font=("bold", 10))
            label_5.place(x=200, y=380)
            list1 = ['Afghanistan', 'Angola', 'Algeria', 'Albania', 'Akrotiri', 'Armenia', 'Austria', 'Australia', 'Azerbaijan', 'American Samoa', 'Antarctica',
                     'Argentina', 'Belgium', 'Borkina Faso', 'Benin', 'Bolivia', 'Botswana', 'Brazil', 'Bulgaria', 'Burundi', 'China', 'Canada', 'Cameroon', 'Chade',
                     'Chile', 'Colombia', 'Comoros', 'Congo Democatic Republic', 'Costa Rica', 'Cape Verde', 'Crotia', 'Cuba', 'Czech Republic', 'India', 'UK', 'Nepal', 'Iceland', 'South Africa', 'Ghana', 'Kenya', 'Germany', 'North Korea',
                     'Netherland', 'Iceland', 'Poland']
            droplist = OptionMenu(root1, country, *list1)
            droplist.config(width=40)
            country.set('Select Country')
            droplist.pack()
            droplist.place(x=320, y=380)
            btn_submit = Button(root1, text='Submit', width=20, bg='red',
                                fg='white', font=(14), command=reg_submit)
            btn_submit.pack()
            btn_submit.place(x=360, y=470)
        
        
        def login():
            pass
        
        
        def window():
            global root
            root = Tk()
            root.geometry("900x700")
            root.title("Welcome Page")
            root.iconbitmap("")
        
            Label(root, text="Welcome To Python Visa Application Portal! \n\nTo check your visa application status, file a new application or update your application, \nLogin or Create an account.",
                  fg="white", bg="grey", height="6", width="900", font=("Calibri", 14)).pack()
            Label(root, text="").pack()
            Label(root, text="").pack()
            Label(root, text="").pack()
            Label(root, text="").pack()
            Label(root, text="").pack()
            Button(root, text="Login", width=20, font=(
                "bold", 14), command=login).pack()
            Label(root, text="").pack()
            Button(root, text="Create Account", width=20,
                   font=("bold", 14), command=register).pack()
            Label(root, text="").pack()
            Label(root, text="").pack()
            Label(root, text="Copyright 2020. All Rights Reserved \nWith Luv From Group 3",
                  font=("Calibri", 8)).pack()
        
            root.mainloop()
        
        
        setup_db()
        window()

【问题讨论】:

  • 使用VALUES (NULL, ?, ?, ?, ?, ?, ?)
  • @acw1668 也不会抛出错误,因为ID 列设置为NOT NULL

标签: python sqlite user-interface tkinter


【解决方案1】:

我实际上认为与其循环遍历列表,不如这样做:

values = fname.get(), email.get(), password.get(),cPassword.get(), sex.get(), country.get()
c.execute("INSERT INTO users(fname,email,password,cPassword,sex,country) VALUES (?, ?, ?, ?, ?, ?)", values)
c.execute('commit')

我在这里所做的只是指定要插入哪一列,并且您的表有 7 列(包括oid)但只给出了 6 个值这一事实可能是问题所在。而且我没有循环,因为我相信@balderman 说这个问题正在调用INSERT 6 次。

让我知道这是否解决了错误

干杯

【讨论】:

    【解决方案2】:

    如果您不提供所有列(此处您不提供 ID),则应在值之前指定列名:

    INSERT INTO users (column1,column2 ,..) VALUES(value1, value2 ,...);

    【讨论】:

      【解决方案3】:

      下面的循环是错误的 - 在这个循环中你调用了 INSERT 6 次。

      每次使用 1 个字符串(姓名、电子邮件、..)。

      您需要调用一次 INSERT 并使用 new_user 作为 c.execute 的第二个参数

      new_user = [fname.get(), email.get(), password.get(),
                              cPassword.get(), sex.get(), country.get()]
              
      for element in new_user:
           c.execute("INSERT INTO users VALUES (?, ?, ?, ?, ?, ?)", (element))
      

      【讨论】:

      • 值只有6但表实际上包含7列不是问题吗?
      • @CoolCloud 阅读代码-element指向的数据是什么?它是用户,电子邮件,密码...... - 对吗?但它是在一个循环中,元素只指向用户的一个属性。
      • 我其实不知道,我添加了一个答案,这可能是问题吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多