2013-03-23 21 views
-1

說我有一個unicode字符串u"Robinson Can\u00f3",我使用下面的存儲在sqlite3的數據庫中的字符串:我怎麼查詢的sqlite3的一個unicode字符串,使用Python

cursor.execute('create table tb (name text)') 
cursor.execute('insert into tb values (?)', str') 

但是,如果我想查詢unicode字符串u「羅賓遜可以從數據庫中取出」,我什麼都沒有。

cursor.execute("select * from tb where name = '%s'" % str) 

但如果我查詢另一個unicode字符串不包含unicode的,比如,U「科比Byrant」,我可以進入回來。

那麼任何人都可以告訴我如何使用Python處理sqlite3的unicode?

非常感謝!

+0

您的查詢不是一個Unicode字符串 – Esailija 2013-03-23 14:43:35

+0

謝謝你,你是什麼意思?我的查詢是u'Robinson可以',這是一個Unicode字符串,對不對? – Idealist 2013-03-23 14:47:46

+0

是的,但至少在其他語言中(其中所有字符串都像python unicode字符串,並且字節數組類似python字符串),所有內容都是unicode字符串,並且只能從I/O上的字節轉換爲/。我覺得因爲你的查詢是字節數組,並且參數是unicode字符串,所以我想它會以某種方式搞亂它。 – Esailija 2013-03-23 14:48:36

回答

3

不是執行SQL代碼時使用字符串插值。改用SQL參數:

cursor.execute("select * from tb where name = ?", (str,)) 

這適用於涉及參數的任何數據庫交互,而不僅僅是SQLLite。

相關問題