2017-06-08 4 views
0

この句を使用すると、すべて正しいです。ここでfindモンキー・D・ルフィPython Django mysqldbでユニコード値を選択

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を使用し、それが文字列の集合ではなく、単に1つの文字列であることを意味します。

+0

ミス "" 文字列の周りに〜>多くのことが "テキスト" NamNguyễ[email protected] –

+0

あなたは 'の変化を意味する(モンキー・D・ルフィ)'( 'への「モンキー・D・ルフィ ")'? – Howard

+0

find.encode( 'utf-8')を試してみましたか? –

答えて

0

は、クエリ

# 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 
+0

私は 'in'を使用します。これは、文字列ではなく、文字列の集合であることを意味します。 – Howard

+0

Django querry(RAW querryではなく)を使用するように指示します。もしそうでなければ、あなたはdjangoのタグを削除してdjangoのタグを取り除くと確信しています... –

+0

タグが削除されました。 – Howard

関連する問題