Python Django mysqldb select with unicode值

2024-09-30 18:22:46 发布

您现在位置:Python中文网/ 问答频道 /正文

当我使用这个条款时,一切都是对的。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, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''(\'\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\')'' at line 1") And for this clause, error happened. find here is モンキー・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表示它是一组字符串,而不仅仅是一个字符串。你知道吗


Tags: nameinagesqlfindgenderselectheight
1条回答
网友
1楼 · 发布于 2024-09-30 18:22:46

查询中可能缺少“”

# 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

相关问题 更多 >