您好,我正在关注Python MYSQL update statement,并设法为我的程序的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你知道我哪里错了吗?
发布于 2018-04-01 07:36:30
看起来您正在查看的帖子是针对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})https://stackoverflow.com/questions/49593427
复制相似问题