【发布时间】:2020-08-22 11:52:28
【问题描述】:
我的一个 sqlite 数据库表出现此错误,我无法解决,即使在网上查找了几个小时后也是如此。我也尝试过删除数据库并从一个新数据库开始,但这不起作用。
错误:第 90 行,在 submit1 中 c.execute("插入响应值 (:company,:roles,:interview,:offer,:wage_offered,:start_date)", sqlite3.OperationalError:表响应有 5 列,但提供了 6 个值
但是有 6 列,所以我不明白为什么会出现此错误。
submit1函数代码如下:
#create submit function for
def submit1():
# Create a databse or connect to one
connection = sqlite3.connect('jobtracker.db')
#Create a cursor
c = connection.cursor()
# Insert into table
c.execute("INSERT INTO responses VALUES (:company, :roles, :interview, :offer, :wage_offered, :start_date)",
{
'company': company1.get(),
'roles': role1.get(),
'interview': interview.get(),
'offer': offer.get(),
'wage_offered': wage_offered.get(),
'start_date': start_date.get()
})
# Commit changes
connection.commit()
# Close connection
connection.close()
这是数据库的代码:
#Creating database
conn = sqlite3.connect('jobtracker.db')
c = conn.cursor()
#Creating tables for database
c.execute("""CREATE TABLE IF NOT EXISTS applications (
company text,
role text,
industry text,
location text,
wage_min integer,
wage_max integer,
start_date integer,
status text
)""")
c.execute("""CREATE TABLE IF NOT EXISTS responses (
company text,
role text,
interview integer,
offer integer
wage_offered integer,
start_date integer
)""")
conn.commit()
conn.close()
#create submit function for
def submit():
# Create a databse or connect to one
connection = sqlite3.connect('jobtracker.db')
#Create a cursor
c = connection.cursor()
# Insert into table
c.execute("INSERT INTO applications VALUES (:company, :roles, :industry, :location, :wage_min, :wage_max, :start_date, :status)",
{
'company': company.get(),
'roles': role.get(),
'industry': industry.get(),
'location': location.get(),
'wage_min': wage_min.get(),
'wage_max': wage_max.get(),
'start_date': start_date.get(),
'status': status.get()
})
# Commit changes
connection.commit()
# Close connection
connection.close()
提前谢谢你。
【问题讨论】:
-
缺少逗号
offer integer -
我看不出代码有什么问题。您的表中有
CREATE TABLE IF NOT EXISTS。您可能已更新代码以添加新列,但由于表已存在,因此数据库未更新。 -
您的
CREATE TABLE语句中有语法错误,因此不能是创建表的代码。 -
是的,在执行 Mike67 评论后,看看你的表的
DESCRIBE <tablename>,如果你真的有 6 列或者是 5 列的话 -
感谢您的意见,非常感谢。原来我通过删除数据库然后仅使用 CREATE TABLE 而不是 CREATE TABLE IF NOT EXISTS 重新创建它来解决它。然后在创建数据库后,我注释掉了代码,所以它没有创建更多的表。
标签: python database sqlite tkinter