0
votes

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

1 Answers

1
votes

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.