1

I have an sqlite3 database that I have created which I'm filling with data from a db2 database. I get an error when I run the following:

for row in db2_connection.execute(query):
    sqlite3_connection.execute("INSERT INTO MY_TABLE VALUES (?, ?, ?)", row)

Here's the error:

File "example.py", line 72, in load_micr_db
    sqlite3_connection.execute("INSERT INTO MY_TABLE VALUES (?, ?, ?)", row)
sqlite3.ProgrammingError: You must not use 8-bit bytestrings unless you use a
text_factory that can interpret 8-bit bytestrings (like text_factory = str). 
It is highly recommended that you instead just switch your application to 
Unicode strings.

This sounds like excellent advice, but I don't know how to follow it. How do I "switch my application to Unicode strings"?

I'm using Python 2.6, pyodbc to query db2 9, sqlite3.

1 Answer 1

3

Use string literals prefixed with a u.

print u'I am a Unicode string.'

"Unicode in Python, Completely Demystified"

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

3 Comments

Can you unpack that? I switched my second line (from the above example to sqlite3_connection.execute(u"INSERT INTO MY_TABLE VALUES (?, ?, ?)", row) and still received the same error. Perhaps I misunderstood your answer?
The elements in row also need to be unicode if they're strings.
My db2 source was returning strings that needed to be decoded to unicode using 'latin-1'. Thanks for your help.

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.