473,395 Members | 2,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

web app breakage with utf-8

Hello, after two days of failed efforts and googling, I thought I had
better seek advice or observations from the experts. I would be grateful
for any input.

We have various small internal web applications that use utf-8 pages for
storing, searching and retrieving user input. They have worked fine for
years with non ASCII values, including Russian, Greek and lots of accented
characters. They still do on an old version of python (2.2.1), and there's
nothing in the code to decode/encode the input, it's *just worked*.

Recently however, while testing on a dev machine, I notice that any
characters outside ASCII are causing SQL statement usage to break with
UnicodeDecodeError exceptions with newer versions of python (2.3 and 2.4).
There are a number of threads online, suggesting converting to unicode
types, and similar themes, but I'm having no success. I am probably
completely misunderstaning something fundamental. :-(

My first question is did something change for normal byte stream usage
making it more strict? I'm surprised there aren't more problems
like this online.

Is there a correct way to handle text input from a <FORMwhen the page is
utf-8 and that input is going to be used in SQL statements? I've tried
things like (with no success):
sql = u"select * from blah where col='%s'" % input

Doing sql = sql.decode('latin1') prior to execution prevents the
some UnicodeDecodeError exceptions, but the data retrieved from the tables
is no longer usable, causing breakage when being used to create the output
for the browser.
I really am at a loss for what is going wrong, when everything works fine
on crusty old 2.2.1. What are others doing for caputre, store, and output
for web utf-8?
Rgds,
Jason

Jul 6 '06 #1
4 1434
elmo wrote:
Hello, after two days of failed efforts and googling, I thought I had
better seek advice or observations from the experts. I would be grateful
for any input.

We have various small internal web applications that use utf-8 pages for
storing, searching and retrieving user input. They have worked fine for
years with non ASCII values, including Russian, Greek and lots of accented
characters. They still do on an old version of python (2.2.1), and there's
nothing in the code to decode/encode the input, it's *just worked*.

Recently however, while testing on a dev machine, I notice that any
characters outside ASCII are causing SQL statement usage to break with
UnicodeDecodeError exceptions with newer versions of python (2.3 and 2.4).
There are a number of threads online, suggesting converting to unicode
types, and similar themes, but I'm having no success. I am probably
completely misunderstaning something fundamental. :-(

My first question is did something change for normal byte stream usage
making it more strict? I'm surprised there aren't more problems
like this online.

Is there a correct way to handle text input from a <FORMwhen the page is
utf-8 and that input is going to be used in SQL statements? I've tried
things like (with no success):
sql = u"select * from blah where col='%s'" % input
What about " ... % unicode(input, "UTF-8")" ?

Doing sql = sql.decode('latin1') prior to execution prevents the
some UnicodeDecodeError exceptions, but the data retrieved from the tables
is no longer usable, causing breakage when being used to create the output
for the browser.

I really am at a loss for what is going wrong, when everything works fine
on crusty old 2.2.1. What are others doing for caputre, store, and output
for web utf-8?
You didn't tell us what database you are using, which encoding your database
uses, which Python-DB interface library you deploy, and lots of other things
that might be helpful to solve your problem.

Stefan
Jul 6 '06 #2
On Thu, 06 Jul 2006 19:16:53 +0200, Stefan Behnel wrote:
>>
Is there a correct way to handle text input from a <FORMwhen the page is
utf-8 and that input is going to be used in SQL statements? I've tried
things like (with no success):
sql = u"select * from blah where col='%s'" % input

What about " ... % unicode(input, "UTF-8")" ?

I guess it's similar, I've had partial success with input.decode('utf-8')
before DB usage, and then output.encode('utf-8') for output. But although
this stores and displays newly added utf-8 texts correctly, it
causes other problems when displaying the existing texts. I think
they're suffering from a double encoding issue. It seems rather
strange the encode/decode appears to be required now, and not before.
Is this how it should be done?
>
You didn't tell us what database you are using, which encoding your
database uses, which Python-DB interface library you deploy, and lots of
other things that might be helpful to solve your problem.
That would be MySQLdb with latin1, but I've tried various methods to make
it utf-8 (lots of guidance online). But this was only after I discovered
the breakage with the newer python. I.e. it has worked for years on both
machines and various python versions. I omitted that info because I can
paste the SQL into mysql's shell, it does the expected thing with no
errors, so I assumed the DB itself isn't the cause. I guess it could
be a new MySQLdb issue causing breakage.
I feel I can see part of the light, but if I'm close to what I think
is needed, it's not practical to change everything to handle encode/decode
site wide, especially as some of the data gets moved to Oracle for other
applications (most is written in Perl).

I'm thinking I need to do this now, is this the norm?:

get user input from web
text.encode('utf-8')
store or use as search in DB
text.decode('utf-8')
display page etc

The encode/decode stages have never been required before :-(

>
Stefan
Jul 6 '06 #3
On Thu, 06 Jul 2006 19:41:32 +0000, elmo wrote:
I guess it could be a new MySQLdb issue causing breakage.
Replying to self, this is *very* close to the problem:
http://sourceforge.net/tracker/index...07&atid=374932
Jul 6 '06 #4
replacing connection.character_set_name instance method seems to work
but is this the correct/preferred way?
>>import MySQLdb
MySQLdb.get_client_info()
'4.1.8'
>>import sys
sys.version
'2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200 32 bit (Intel)]'
>>sys.platform
'win32'
>>cred = {'passwd': 'secret', 'host': 'myhost', 'db': 'mydb', 'user': 'justin'}
insert = 'insert into unicodetest2 (foo) values (%s)'
alpha = u'\N{GREEK SMALL LETTER ALPHA}'
alpha
u'\u03b1'
>>conn.close()
conn = MySQLdb.connect(**cred)
cur = conn.cursor()
cur.execute(insert, 'a')
1L
>>conn.commit()
conn.close()
conn = MySQLdb.connect(**cred)
cur = conn.cursor()
cur.execute(select)
1L
>>cur.fetchall()
((9L, 'a'),)
>>conn.close()
conn = MySQLdb.connect(**cred)
cur = conn.cursor()
cur.execute(insert, alpha)
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "E:\Python23\Lib\site-packages\MySQLdb\cursors.py", line 95, in
execute
return self._execute(query, args)
File "E:\Python23\Lib\site-packages\MySQLdb\cursors.py", line 114, in
_execute
self.errorhandler(self, exc, value)
File "E:\Python23\Lib\site-packages\MySQLdb\connections.py", line 33,
in defaulterrorhandler
raise errorclass, errorvalue
LookupError: unknown encoding: latin1_swedish_ci
>>conn.close()
conn = MySQLdb.connect(**cred)
def character_set_name(*args, **kwargs): return 'utf-8'
....
>>character_set_name()
'utf-8'
>>conn.close()
conn = MySQLdb.connect(**cred)
conn.character_set_name()
'latin1_swedish_ci'
>>import new
conn.character_set_name = new.instancemethod(character_set_name, conn, conn.__class__)
conn.character_set_name()
'utf-8'
>>cur = conn.cursor()
cur.execute(insert, alpha)
1L
>>conn.close()
conn = MySQLdb.connect(**cred)
conn.character_set_name()
'latin1_swedish_ci'
>>cur = conn.cursor()
cur.execute(select)
2L
>>cur.fetchall()
((10L, '\xce\xb1'), (9L, 'a'))
>>conn.close()
conn = MySQLdb.connect(unicode='utf-8', **cred)
conn.character_set_name()
'latin1_swedish_ci'
>>cur = conn.cursor()
cur.execute(select)
2L
>>cur.fetchall()
((10L, u'\u03b1'), (9L, u'a'))
>>conn.close()
conn = MySQLdb.connect(**cred)
cur = conn.cursor()
cur.execute(select)
2L
>>cur.fetchall()
((10L, '\xce\xb1'), (9L, 'a'))
>>conn.close()
Jul 7 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: lawrence | last post by:
Someone on www.php.net suggested using a seems_utf8() method to test text for UTF-8 character encoding but didn't specify how to write such a method. Can anyone suggest a test that might work?...
4
by: Alban Hertroys | last post by:
Another python/psycopg question, for which the solution is probably quite simple; I just don't know where to look. I have a query that inserts data originating from an utf-8 encoded XML file....
12
by: Mike Dee | last post by:
A very very basic UTF-8 question that's driving me nuts: If I have this in the beginning of my Python script in Linux: #!/usr/bin/env python # -*- coding: UTF-8 -*- should I - or should I...
38
by: Haines Brown | last post by:
I'm having trouble finding the character entity for the French abbreviation for "number" (capital N followed by a small supercript o, period). My references are not listing it. Where would I...
6
by: jmgonet | last post by:
Hello everybody, I'm having troubles loading a Xml string encoded in UTF-8. If I try this code: ------------------------------ XmlDocument doc=new XmlDocument(); String s="<?xml...
6
by: archana | last post by:
Hi all, can someone tell me difference between unicode and utf 8 or utf 18 and which one is supporting more character set. whic i should use to support character ucs-2. I want to use ucs-2...
7
by: Jimmy Shaw | last post by:
Hi everybody, Is there any SIMPLE way to convert from UTF-16 to UTF-32? I may be mixed up, but is it possible that all UTF-16 "code points" that are 16 bits long appear just the same in UTF-32,...
10
by: Jed | last post by:
I have a form that needs to handle international characters withing the UTF-8 character set. I have tried all the recommended strategies for getting utf-8 characters from form input to email...
23
by: Allan Ebdrup | last post by:
I hava an ajax web application where i hvae problems with UTF-8 encoding oc chineese chars. My Ajax webapplication runs in a HTML page that is UTF-8 Encoded. I copy and paste some chineese chars...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.