MySQL参数化查询我很难使用MySQLdb模块将信息插入到我的数据库中。我需要在表中插入6个变量。cursor.execute ("""
INSERT INTO Songs (SongName, SongArtist, SongAlbum, SongGenre, SongLength, SongLocation)
VALUES
(var1, var2, var3, var4, var5, var6)
""")有谁能帮我解决这里的语法问题吗?
5 回答
千巷猫影
TA贡献1829条经验 获得超7个赞
不正确(涉及安全问题)
c.execute("SELECT * FROM foo WHERE bar = %s AND baz = %s" % (param1, param2))
正确(带转义)
c.execute("SELECT * FROM foo WHERE bar = %s AND baz = %s", (param1, param2))
printf
python-sqlite
).
茅侃侃
TA贡献1842条经验 获得超21个赞
some_dictionary_with_the_data = { 'name': 'awesome song', 'artist': 'some band', etc...}cursor.execute (""" INSERT INTO Songs (SongName, SongArtist, SongAlbum, SongGenre, SongLength, SongLocation) VALUES (%(name)s, %(artist)s, %(album)s, %(genre)s, %(length)s, %(location)s) """, some_dictionary_with_the_data)
青春有我
TA贡献1784条经验 获得超8个赞
cursor.execute (""" UPDATE animal SET name = %s WHERE name = %s """, ("snake", "turtle")) print "Number of rows updated: %d" % cursor.rowcount
cursor.execute (""" INSERT INTO Songs (SongName, SongArtist, SongAlbum, SongGenre, SongLength, SongLocation) VALUES (%s, %s, %s, %s, %s, %s) """, (var1, var2, var3, var4, var5, var6))
添加回答
举报
0/150
提交
取消