Yet another problem with special characters (Ä, Ö, Ü, etc.)

Steve Holden sholden at holdenweb.com
Thu Oct 10 07:52:40 EDT 2002


"Dominik Reiter" <dominik.reiter at epost.de> wrote ...
> Thank you for your answer.
> The problem is how my data gets written into the database. Here's a little
> code snippet:
>
> ## Start Code here
>
> #!/usr/bin/python
>
> import cgi
> import ...
> import connective  #returns the cursor
>
> form = cgi.FieldStorage()
> val = ()
> key = ""
> for name in form.keys():
>     inh = cgi.escape(form[name].value)
>     if name == 'enter':
>         break
>     val = val + (inh,)
>     key = key + name + ", "
>     dict[name] = inh
> key = key[:-2]
> cursor = connective.connect()
> cursor.execute('INSERT INTO %s (%s) VALUES %s' % (tab, key, val))
>
> ## End code here
>
> Everything works ok, but in the form you can enter a city or country -->
if
> you enter 'Österreich', the value in the corresponding field in the
database
> now reads '\xd6sterreich'. If I execute a select-statement and print the
> results it stays '\xd6sterreich'. This is very annoying, if you know what
I
> mean ;-)
>
> Do you have any idea how to handle this problem ??

It could be a bug in your database module - I've never heard of
"connective", where did it come from (unfortunately Googling for "python
connective database module" didn't give helpful results).

Normally one creates a database connection with the .connect() module, then
creates a cursor with connection.cursor(); if you are using the module
correctly then it's perhaps not DB API compliant, but that hardly matters.
What database back end are you using? Is the field defined as a Unicode
field or as plain ASCII?

Most likely you are simply having trouble interpreting your results. Note
that whether you see the hex escape depends on whether Python uses repr() or
str() to produce what you are printing. Here's a little test program that
shows you Python databases *can* work with these strange characters (modulo
any text wrapping my mailer might do). I seem to remember HoldenWebSQL is an
Access database (blush) ...

#
# Database test
#
import mx.ODBC.Windows as db

conn = db.connect("HoldenWebSQL")
curs = conn.cursor()

curs.execute("CREATE TABLE tst (txt VARCHAR(20) PRIMARY KEY)")
curs.execute("INSERT INTO tst VALUES ('Österreich')")
curs.execute("SELECT * FROM tst")
result = curs.fetchall()
print result
country = result[0][0]
print country, len(country), country[0]
curs.execute("DROP TABLE tst")
# End

So, what happens when I run this? I get ...

C:\Steve\Projects\Python>python dbtest3.py
[('\xd6sterreich',)]
Österreich 10 Ö

In  other words, when I print the list of tuples returned by the fetchall(),
Python uses repr() to print the tuple components. When I extract the
individual string, Python uses str() and all is well.

Of course, now you have to decide whether you really are seeing a bug or
not, and (if not) 'fess up! Hope this helps.

regards
-----------------------------------------------------------------------
Steve Holden                                  http://www.holdenweb.com/
Python Web Programming                 http://pydish.holdenweb.com/pwp/
Previous .sig file retired to                    www.homeforoldsigs.com
-----------------------------------------------------------------------






More information about the Python-list mailing list