Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python SQL更新语法问题_Python_Sqlite_Sql Parametrized Query - Fatal编程技术网

Python SQL更新语法问题

Python SQL更新语法问题,python,sqlite,sql-parametrized-query,Python,Sqlite,Sql Parametrized Query,您好,我正在跟踪并设法为我的程序的SQL Update with variables函数生成以下代码: def editInfo(start, userName): newFavGenre = input("Enter your favourite genre") newFavArtist = input("Enter your favourite artist") ## userName is a global variable con = lite.conne

您好,我正在跟踪并设法为我的程序的SQL Update with variables函数生成以下代码:

def editInfo(start, userName):
    newFavGenre = input("Enter your favourite genre")
    newFavArtist = input("Enter your favourite artist")
    ## userName is a global variable
    con = lite.connect(db)
    cur = con.cursor()
    cur.execute ("""
    UPDATE users
    SET favGenre=%s, favArtist=%s
    WHERE username=%s
""", (newFavGenre, newFavArtist, userName))
    results = cur.fetchall()
    return result
    mainmenu()
并不断体验此错误代码:

sqlite3.OperationalError: near "%": syntax error

你知道我哪里出了问题吗?

看来你正在看的帖子是针对MySQL的,基于你的错误,我猜想你正在使用
sqlite3
python接口

看着

应该是

cur.execute("""
    UPDATE users
    SET favGenre=?, favArtist=?
    WHERE username=?
    """, (newFavGenre, newFavArtist, userName))
您还可以使用它们的命名样式,而不是使用元组,而是使用字典

cur.execute("""
    UPDATE users
    SET favGenre=:genre, favArtist=:artist
    WHERE username=:username
""", {'genre':newFavGenre, 'artist': newFavArtist, 'username':userName})
cur.execute("""
    UPDATE users
    SET favGenre=:genre, favArtist=:artist
    WHERE username=:username
""", {'genre':newFavGenre, 'artist': newFavArtist, 'username':userName})