【问题标题】:"Table has X columns but Y values were supplied" error“表有 X 列,但提供了 Y 值”错误
【发布时间】:2020-08-25 21:05:49
【问题描述】:

这是一个 python 脚本,它使用 TKinter 创建一个菜单,然后应该与使用 SQLite 制作的数据库进行通信

class ConectarBD:
    def __init__(self):
        self.con = sqlite3.connect('db.sqlite3')
        self.cur = self.con.cursor()SQL
        self.criar_tabela()

def criar_tabela(self):
        self.cur.execute('''CREATE TABLE IF NOT EXISTS MuseuSoftware (
        nome TEXT, 
        autor TEXT, 
        tipo INTEGER, 
        estado TEXT);''')

def inserir_registro(self, usuario)
        self.cur.execute('''INSERT INTO MuseuSoftware VALUES (?, ?, ?, ?)''', usuario)

稍后我在“inserir_registro”处填写“usuario”变量并执行方法...

    def cclicked():
        nome_arte = nome_entry.get()
        nome_autor = autor_entry.get()
        estado = estado_entry.get()
        tipo_entrada = tipo.get()

        entrada = (nome_arte, nome_autor, tipo_entrada, estado)

        banco.inserir_registro(usuario=entrada)


    create_bttn = Button(tab1, text='Adicionar', command=cclicked)
    create_bttn.grid(column=0, row=6)

但是它显示了一条错误消息,说“Table MuseuSoftware 有 1 列,但提供了 4 个值”,即使我确保在“CREATE TABLE”部分写了 4 列。

对任何明显的错误深表歉意,因为我才刚刚开始编码。

【问题讨论】:

  • 请提供minimal reproducible example,以及整个错误输出。
  • @AMC 这够好吗? “Table MuseuSoftware 有 1 列,但提供了 4 个值”是我收到的全部错误输出。
  • 暂时忽略python,你能显示运行SQLite命令'.schema MuseuSoftware'的输出吗
  • 表示只有1列而不是4列,可能是你建表的时候出错了,我建议你删除表重新建表。可能是初始创建时出错,但由于IF NOT EXISTS 它没有创建一个有 4 列的新表
  • 我在休息了一晚后自己发现了错误。事实证明,在最初的几次测试和编辑之后,我并没有删除该表,因此它有剩余的数据与创建新表相冲突......糟糕!

标签: python sql database sqlite


【解决方案1】:

我遇到了几乎相同的错误,我在客户旁边添加了一个额外的“(

c.execute("""CREATE TABLE clients("
    first_name TEXT,
    second_name TEXT,
    email TEXT,
    dni TEXT,
    ")""") 

删除它就可以了

c.execute("""CREATE TABLE clientes(
first_name TEXT,
second_name TEXT,
email TEXT,
dni TEXT,
)""") 

【讨论】:

    猜你喜欢
    • 2022-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多