When I want to insert a string from Python code to SQLite database I get this error:
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 is the insert statement:
cur.execute("insert into links (url, title, ...) values (:url, :title, ...)", locals())
The string came into existence as follows:
soup = BeautifulSoup(html.read(), fromEncoding="utf-8")
html.close()
for i in soup.findAll('a'):
url = i['href']
title = i.renderContents()
Could you advise me how to insert the string into SQLite database?
EDIT: I found out that url string was OK when inserting to another table. The type of url string was unicode. The problem is when inserting title string. The type of title string is str.
I tried:
title = unicode(i.renderContents())
but this ends with error:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 44: ordinal not in range(128)
thank you
href=Truekeyword parameter; there could beaelements withouthrefattribute. You might getKeyErrorotherwise 2. Usage oflocals()outside debugging is frowned upon.KeyErrorthough this wasn't the case. Why is usage oflocals()not recommended outside debugging?locals()either hides problems with the structure of your code or at best it makes the code less readable and more brittle.