1

I have a problem with encoding in SQLite (sqlite3 module in Python 2.7). I have a table with two columns: (text_number int, word text). In word we have numbers, English and Russian words.

I need to find all strings in table where word = search_word.

When search_word is number, I have no problems:

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

and gives something like (361, u'146').

But, any other kind of characters do not work:

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

I try many versions of decode and so on, but nothing can help me.

1
  • Are you using the sqlite3 package? Commented Dec 25, 2012 at 16:54

1 Answer 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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.