SQLITE3 DB数据从同一个表和同一行中的一个字段传输到另一个字段

时间:2016-02-21 08:58:26

标签: python sqlite

我试图将同一行中的数据从一个字段移动到另一个字段。 这是我的代码,但它不适用于更新语句:

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")

我在python中运行的错误是:

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.

1 个答案:

答案 0 :(得分:0)

cursor.execute需要一个可迭代的。当你给它一个字符串时,执行将其视为5项可迭代(5个字符)。 将执行行更改为

cursor.execute(sql, (Name,))