当我使用此子句时,一切都正确。这里的findモンキー・D・ルフィ

   sql_fmt = "select name, gender, age, height, hobby, ability, popularity, voice_actor, birth_place, image from characters where name in ('%s');" % find.encode('utf-8')
   self.db.execute(sql_fmt)


但是当我使用此子句时,发生了错误。这里的find(モンキー・D・ルフィ)('モンキー・D・ルフィ')

sql_fmt = "select name, gender, age, height, hobby, ability, popularity, voice_actor, birth_place, image from characters where name in %s;"
self.db.execute(sql_fmt, find)


错误:


  ProgrammingError:(1064,“您的SQL语法有错误;请查看与您的MySQL服务器版本相对应的手册,以获取在''(\'\ xe3 \ x83 \ xa2 \ xe3 \ x83 \ xb3 \第1行的xe3 \ x82 \ xad \ xe3 \ x83 \ xbc \ xe3 \ x83 \ xbbD \ xe3 \ x83 \ xbb \ xe3 \ x83 \ xab \ xe3 \ x83 \ x95 \ xe3 \ x82 \ xa3 \')'' )
  对于此子句,发生了错误。 find这是モンキー・D・ルフィ


sql_fmt = "select name, gender, age, height, hobby, ability, popularity, voice_actor, birth_place, image from characters where name in ('%s');"
self.db.execute(sql_fmt, find)


对于此子句,一切顺利。 find这是モンキー・D・ルフィ

sql_fmt = "select name, gender, age, height, hobby, ability, popularity, voice_actor, birth_place, image from characters where name in (%s);"
self.db.execute(sql_fmt, find)


注意 :

我使用in表示它是一组字符串,但不仅仅是一个字符串。

最佳答案

可能是您在查询中缺少“”

# it should be:
select * from characters where name = "some string"
#OR
select * from characters where name in "some string"
# In your case:
sql_fmt = "select name, gender, age, height, hobby, ability, popularity, voice_actor, birth_place, image from characters where name in "%s";"


或者可能是某种形式,我不使用它。

您应该使用:

res = Models.Objects.filter(name__in = yourlist)
# Django help you all the way to convert it to Query

10-06 08:40