在我的 Cur4.execute 中,我尝试将艺术家姓名与我之前从 cur3 获得的艺术家姓名进行比较 (name = row[1]), 但我得到了错误:
Traceback (most recent call last):
File "mp2.py", line 49, in <module>
cur4.execute('SELECT * FROM artist WHERE artist.name = %s',name)
File "C:\python27\lib\site-packages\MySQLdb\cursors.py", line 187, in execute
query = query % tuple([db.literal(item) for item in args])
TypeError: not all arguments converted during string formatting
我该如何解决这个问题?
cur3 = db.cursor()
cur3.execute("SELECT image.link, artist.name, detail.detail_id, FROM artist, detail, image WHERE image.artist_id = artist.artist_id AND detail.image_id = image.image_id LIMIT 1")
ans = cur3.fetchall()
for row in ans:
print row[1:]
name = row[1]
db.close()
cur4 = db.cursor()
cur4.execute('SELECT * FROM artist WHERE artist.name = %s',name)
最佳答案
我不是专家,但也许可以尝试用括号将字符串格式区域括起来,而不是 cur4.execute('SELECT * FROM Artist WHERE Artist.name = %s',name)
得到这个:
cur4.execute(('从艺术家中选择* WHERE艺术家.name = %s',name))
编辑:实际上,我认为 mshsayem 是对的(原始问题的评论)
关于python - 如何在python中执行sql时使用字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46869952/