Python Django mysqldb选择unicode值

时间:2017-06-08 04:16:56

标签: python mysql unicode

当我使用这个条款时,一切都是正确的。这里的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服务器版本对应的手册,以便在#39;('(')附近使用正确的语法39; \ XE3 \ X83 \ XA2 \ XE3 \ X83 \ XB3 \ XE3 \ X82 \ XAD \ XE3 \ X83 \ XBC \ XE3 \ X83 \ xbbD \ XE3 \ X83 \ XBB \ XE3 \ X83 \ XAB \ XE3 \ X83 \ X95 \ xe3 \ x82 \ xa3 \')''在第1行")   对于这个条款,错误发生了。 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表示它是一组字符串,但不仅仅是一个字符串。

1 个答案:

答案 0 :(得分:0)

可能你错过了#34;"在查询中

# 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