您好我正在关注Python MYSQL update statement并设法为我的程序的SQL Update生成带有变量函数的代码:
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
我出错的任何想法?
答案 0 :(得分:2)
看来你正在看的帖子是针对MySQL的,我推测你是根据你的错误使用sqlite3
python接口。
查看sqlite3 docs ...
cur.execute("""
UPDATE users
SET favGenre=%s, favArtist=%s
WHERE username=%s
""", (newFavGenre, newFavArtist, userName))
应该是
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})