1

sqlite3我在 SQLite( Python 2.7 中的模块)中的编码有问题。我有一个包含两列的表:(text_number int,word text)。我们word有数字、英语和俄语单词。

我需要在 word = search_word 的表中找到所有字符串。

当 search_word 是数字时,我没有问题:

search_word = '146'
lenta_u_w = c.execute('SELECT * FROM lenta WHERE word = ' + search_word)
for w in lenta_u_w :
    print w

并给出类似的东西(361, u'146')

但是,任何其他类型的字符都不起作用:

search_word = u'android'
lenta_u_w = c.execute('SELECT * FROM lenta WHERE word = ' + search_word)
for w in lenta_u_w :
    print w
>>>lenta_u_w = c.execute('SELECT * FROM lenta WHERE word = ' + search_word)
OperationalError: no such column: android

我尝试了许多版本的解码等,但没有什么能帮助我。

4

1 回答 1

1

Use parameters when constructing and executing your queries, which will do the appropriate quoting and escaping for you:

c.execute('SELECT * FROM lenta WHERE word = ?', (search_word,))

See the documentation for the Cursor class in the sqlite3 package for more details.

于 2012-12-25T16:57:29.283 回答