Understanding Unicode & encodings

Jim jhefferon at smcvt.edu
Sun Jul 23 10:11:37 EDT 2006


Raphael.Benedet at gmail.com wrote:
> Hello,
>
> For my application, I would like to execute an SQL query like this:
> self.dbCursor.execute("INSERT INTO track (name, nbr, idartist, idalbum,
> path) VALUES ('%s', %s, %s, %s, '%s')" % (track, nbr, idartist,
> idalbum, path))
No, I'll bet that you'd like to run something like
  self.dcCursor.execute("INSERT INTO track (name, nbr, idartist,
idalbum,path) VALUES (%(track)s, %(nbr)s,
%(idartist)s,%(idalbum)s,'%(path)s')",
{'track':track,'nbr':nbr,'idartist':idartist,'idalbum':idalbum,'path':path})
(only without my typos).  That's an improvment for a number of reasons,
one of which is that the system will quote for you, for instance in
idartist="John's Beer" changing the single quote to two single quotes
to suit SQL.
> Every time I execute this, I get an exception like
> this:
>
> UnicodeDecodeError: 'ascii' codec can't decode byte 0xa1 in position
> 64: ordinal not in range(128)
>
> I tried to encode the different variables in many different encodings
> (latin1), but I always get an exception. Where does this ascii codec
> error comes from? How can I simply build this query string?
Some more information may help: is the error returned before or during
the execute call?  If before, then the execute() call is a distraction.
 If during, then what is your dB, what is it's encoding (is the dB
using latin1, or does the dB only accept ascii?), and what are you
using to connect to it?

Jim




More information about the Python-list mailing list