SQLITE3从一个字段到另一个字段在同一个表和同一行中的数据传输
问题描述:
我试图将数据从一个字段移动到另一个字段。 这是我的代码,但它不与更新语句的工作:SQLITE3从一个字段到另一个字段在同一个表和同一行中的数据传输
def update_ondemanddrama(Name):
with sqlite3.connect("sky_ondemand.db") as db:
cursor = db.cursor()
sql = "update TVshowsDrama set SecLastEp=LastEp where Name=?"
cursor.execute(sql, Name)
db.commit()
工作
def insert_ondemanddrama(values):
with sqlite3.connect("sky_ondemand.db") as db:
cursor = db.cursor()
sql = "update TVshowsDrama set Name=?, LastEp=? where Name=?"
cursor.execute(sql,values)
db.commit()
def insert_ondemanddoc(values):
with sqlite3.connect("sky_ondemand.db") as db:
cursor = db.cursor()
sql = "update TVshowsDoc set Name=?, LastEp=? where Name=?"
cursor.execute(sql,values)
db.commit()
Type = int(input("Doc (1) or Drama (2)"))
Name = input("Enter name of Show")
LastEp = input("Enter Last episode aired (ex. s1e4)")
if Type == 1:
if __name__== "__main__":
show = (Name, LastEp, Name)
insert_ondemanddoc(show)
elif Type == 2:
if __name__== "__main__":
show = (Name, LastEp, Name)
update_ondemanddrama(Name)
insert_ondemanddrama(show)
elif Type >=3:
print ("Incorrect entry")
的错误我得到蟒蛇运行是这样的:
Traceback (most recent call last): File "C:\Users\ict\Downloads\skyondemandv1.py", line 65, in <module>
update_ondemanddrama(Name) File "C:\Users\ict\Downloads\skyondemandv1.py", line 34, in
update_ondemanddrama cursor.execute(sql, Name) sqlite3.ProgrammingError: Incorrect number of bindings supplied.
The current statement uses 1, and there are 5 supplied.
答
光标.execute期望一个迭代。当你给它一个字符串时,执行将它看作是一个可迭代的5个项目(5个字符)。 将执行行更改为
cursor.execute(sql, (Name,))
谢谢。这工作!它为什么在一个额外的圆括号中与Name一起使用?我还是不太明白。 – user5958076
使用圆括号参数是元组(Name),而不是字符串Name。迭代第一个时,你会得到一个单一的字符串。当迭代第二个时,你会得到5个字符。 – noamk
现在我收到了一个不同的错误,我确信程序的基础使用相同的逻辑。错误消息如下:回溯(最近最后调用): 文件 “”,第1行,在 测试() 文件 “G:/test.py”,线路33,在测试 update_temp3(值) NameError:全局名称“价值”没有定义 –
user5958076