在Python中从sqlite3查询和提取Unicode值

时间:2012-04-17 16:22:15

标签: python unicode sqlite indic

我在尝试在python中提取存储在sqlite3数据库中的utf-8编码值时遇到问题。

>> import sqlite3
>> connection=sqlite3.connect('mySQLite3DB.db')
>> cursor=connection.cursor()
>> word = unichr(2675)+unichr(37) # ੳ%
>> cursor.execute('select distinct col1 from table1 where col1 like ? limit 3', word)
---------------------------------------------------------------------------
ProgrammingError                          Traceback (most recent call last)
/Users/punjcoder/code/<ipython-input-10-358f7ffe8df0> in <module>()
----> 1 cursor.execute('select distinct col1 from table1 where col1 like ? limit 3', word)

ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 2 supplied.

现在,如果我通过手动插入unicode来运行查询,它就会运行。但是我无法检索文本,而是缓冲区ptr。

>> cursor.execute('select distinct col1 from table1 where col1 like "ੳ%" limit 3')
<sqlite3.Cursor at 0x10dab8500>
>> for row in cursor.fetchall():
>>      print row
(<read-write buffer ptr 0x10dabf898, size 3 at 0x10dabf858>,)

我已经看过下面的链接,但似乎无法找到让它工作的方法。我正在研究Python 2.7.2和SQLite 3.7.10。提前感谢您的帮助。

1 个答案:

答案 0 :(得分:3)

尝试:

 cursor.execute('select distinct col1 from table1 where col1 like ? limit 3', [word])

我希望您将unicode字符串word视为可迭代并分别查看每个字符。