Python SQL更新语法问题
您好,我正在跟踪并设法为我的程序的SQL Update with variables函数生成以下代码: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
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})