Python-sqlite3 错误

问题描述 投票:0回答:1

我正在创建一个数据库管理控制台,我在代码中遇到了问题:

import sqlite3 as sql
name = input('    Enter name of data-base:')
name_tb = input("    Enter name of table:")
print("    Enter values separated by commas in column order!\n If the variable type is string, then enter it in quotation marks <'value'>\n If the variable type is integer(int), then enter without quotation marks <value>")
values = input('    Enter values: ')
l=values.split(',')
state = name[-3:]
if not state == '.db':
    name += '.db'
con_val=''
for el in range(len(l)):
    con_val+='?,'
con_val=con_val[:-1]
con_val='('+con_val+')'
values='('+values+')'
command=("INSERT INTO "+name_tb+" VALUES"+con_val+","+values)
print(command)
values=(1,'ads',23)
db = sql.connect(name)
cur = db.cursor()
cur.execute(command)
db.commit()
db.close()

我尝试了很多代码变体,但不断收到错误!请告诉我出了什么问题以及如何解决

我尝试组合部分命令并添加引号和括号。错误:sqlite3.ProgrammingError:提供的绑定数量不正确。当前语句使用 3,并且提供了 0 个。 sqlite3.输入不完整错误等

python python-3.x syntax-error sqlite3-python
1个回答
0
投票

这些值必须是

cur.execute()
的第二个参数,您不能将其连接到查询字符串。

import sqlite3 as sql
name = input('    Enter name of data-base:')
name_tb = input("    Enter name of table:")
print("    Enter values separated by commas in column order!\n If the variable type is string, then enter it in quotation marks <'value'>\n If the variable type is integer(int), then enter without quotation marks <value>")
values = input('    Enter values: ')
l=values.split(',')
state = name[-3:]
if not state == '.db':
    name += '.db'
con_val=','.join(['?'] * len(l)
con_val='('+con_val+')'
command=(f"INSERT INTO {name_tb} VALUES {con_val}"
print(command)
db = sql.connect(name)
cur = db.cursor()
cur.execute(command, l)
db.commit()
db.close()
© www.soinside.com 2019 - 2024. All rights reserved.