sqlite3.OperationalError: table book 有 6 列,但提供了 5 个值 [英] sqlite3.OperationalError: table book has 6 columns but 5 values were supplied

查看:37
本文介绍了sqlite3.OperationalError: table book 有 6 列,但提供了 5 个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出现错误:

Traceback (most recent call last):
  File "frontend.py", line 2, in <module>
    import backend
  File "/Users/egonsaks/Desktop/Demo/backend.py", line 28, in <module>
    insert("The sea", "John Tablet", 1918, 987654331)
  File "/Users/egonsaks/Desktop/Demo/backend.py", line 15, in insert
    cur.execute("INSERT INTO book VALUES (NULL,?,?,?,?)",(title, author, year, isbn))
  sqlite3.OperationalError: table book has 6 columns but 5 values were supplied

在数据库的表中插入数据时,我想要带有主键整数的 id 列.我根据教程检查了我的代码,并在堆栈溢出中四处寻找类似问题的解决方案,但没有找到解决方案.测试了一些东西,比如删除一个空值,但它说提供了 6 列和 4 个值.

I want id column with primary key integer while inserting data in the table in the database. I checked my code against the tutorial and looked around in stack overflow for a solution from similar problems, but didn't find a solution. Tested some things like removing a null, but then it says 6 columns and 4 values were supplied.

代码在这里:

import sqlite3

def connect():
    conn = sqlite3.connect("books.db")
    cur = conn.cursor()
    cur.execute("CREATE TABLE IF NOT EXISTS book (id INTEGER PRIMARY KEY, 
    title text, author text, year integer, isbn integer)") 
    conn.commit() 
    conn.close()

def insert(title, author, year, isbn):
    conn = sqlite3.connect("books.db") 
    cur = conn.cursor()
    cur.execute("INSERT INTO book VALUES (NULL,?,?,?,?)",(title, author, year, isbn))
    conn.commit()
    conn.close()

推荐答案

请在插入时指定您的列,这很重要,是的,不要包含 NULL 的任何值id 列!

Please specify your columns when inserting, this is important, and yes do not include a NULL or any value for the id column!

cur.execute("INSERT INTO book(title, author, year, isbn) VALUES (?,?,?,?)",
                             (title, author, year, isbn))

这篇关于sqlite3.OperationalError: table book 有 6 列,但提供了 5 个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆