Reputation: 71
I'm using cgi, python 2.7 and sqlite.
I'm trying to insert an string? that contains the characters å,ä and ö into an sqlite table.
the string are taken from an html post using cgi
this code in takes the value:
print '<form action="./printusername.cgi" method="post">'
print 'First Name: <input type="text" name="first_name">'
print '</form>'
im receiving the value in printusername.cgi like this:
import cgi
cgitb.enable()
form = cgi.FieldStorage()
first_name = form.getvalue('first_name')
then i'm trying to pass it to an sqlite table, along whit some other values, like this:
import sqlite3
con = sqlite3.connect('Addressbook.db')
cursor = con.cursor()
contact = (first_name,other_valu_1,other_valu_1)
cursor.execute("INSERT INTO entrys VALUES(?, ?, ?)",contact)
when i'm doing this I'll receive the following error:
<class '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.
if I don't use å,ä or ö in the html post everything is working fine.
why is this error occurring? how can i avoid it and still keep the data in the sqlite table readable?.
if the data need to be formated, how can formate it back when 'im accessing it the next time?
all help is appreciated! sry 4 my bad eng.
Upvotes: 0
Views: 213
Reputation: 1123480
Your browser posts encoded strings to the server. What encoding it uses depends on how the original webpage was encoded, but usually that means UTF-8. You'll need to decode those values to unicode
objects using that encoding.
Your best bet is to tell the browser what codec to use when sending the HTML form, by adding the content encoding to the Content-Type
header:
print 'Content-Type: text/html; charset=utf-8'
and then on receipt of the data, try to decode the data:
try:
first_name = form.getvalue('first_name').decode('utf8')
except (AttributeError, UnicodeDecodeError):
# Could not decode or there was no such field
# Handle the error.
You may want to read up on Python and Unicode:
The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!) by Joel Spolsky
Pragmatic Unicode by Ned Batchelder
Upvotes: 2