0

I'm trying to insert a utf-8 string, but I get:

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.

Here is the whole module. Here are relevant parts of code:

########### reading json from a utf-8 file:
        with open(file_name) as f:
            return json.loads(f.read())
########### feeding utf-8 encoded text to constructor
    for obveznik in json:
        vlasnici[obveznik["id_obveznik"]] = Vlasnik(obveznik["id_obveznik"], obveznik["naziv"].encode('utf-8'))
########### constructor simply assigns
class Vlasnik
    def __init__(self, id, ime):
        self._id = id
        self._ime = ime
########### here's a getter in the same class:
    def ime(self):
        return self._ime
########### and then I use the getter
                        cur.execute("INSERT INTO indeksi VALUES(?, ?, ?, ?, ?, ?)", (
# [...]
                            vlasnik.ime(),
# [...]
                        ))
########################

I get the sqlite3.ProgrammingError on cur.execute(). I read the string frmo utf-8 file and assigned it to a class field using .encode('utf-8'). What more do I have to do? :-/

Thanks in advance.

1 Answer 1

1

If your string is already UTF-8 then do not use encode, just pass it without processing. If not, use .decode("utf-8").

When using Python 2.x consider always using unicode strings. When it's about files IO, consider using the io module instead of the built-in open function, which lets you indicate a specific enconding.

import io
f = open("file.txt", enconding="utf-8")

Hope it helps.

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

1 Comment

That's it, con.execute(... vlasnik.ime().decode('utf-8') ...), that's what I missed. Thanks!

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.