[求助]关于debian环境下通过MySQLdb查询数据库时UnicodeDecodeError的问题

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

[求助]关于debian环境下通过MySQLdb查询数据库时UnicodeDecodeError的问题

弦灵风

最近在写些操作mysql数据的东西,发现写出来后windows下运行正常,但只要迁到debian环境下就无法查询带有中文的数据库了;

conn = MySQLdb.connect(host = host, db=db, user=user,
              passwd=passwd, use_unicode=1, charset='utf8')
cursor = conn.cursor()
query = "select * from tab where su = '待'"
cursor.execute(query)
row1=cursor.fetchall()

错误信息:
  File "/usr/lib/python2.4/site-packages/MySQLdb/cursors.py", line 147, in execute
    query = query.encode(charset)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xb4 in position 35: ordinal not in range(128)


各位高手,这个是为什么啊?


也试过把 MySQLdb的cursor。execute改成
def execute(self, query, char="utf-8", args=None):
        charset = char
    query = query.encode(charset)

最终还是不行,数据库切切实实是utf8的啊。
       
= = = = = = = = = = = = = = = = = = = =
                       

        致
礼!
 



李李
2008-01-21


_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
Reply | Threaded
Open this post in threaded view
|

Re: [求助]关于debian环境下通过MySQLdb查询数据库时UnicodeDecodeError的问题

Jiahua Huang
在你代码开头加

import sys
reload(sys)
sys.setdefaultencoding('utf8')


2008/1/21 李李 <[hidden email]>:

>
> 最近在写些操作mysql数据的东西,发现写出来后windows下运行正常,但只要迁到debian环境下就无法查询带有中文的数据库了;
>
> conn = MySQLdb.connect(host = host, db=db, user=user,
>               passwd=passwd, use_unicode=1, charset='utf8')
> cursor = conn.cursor()
> query = "select * from tab where su = '待'"
> cursor.execute(query)
> row1=cursor.fetchall()
>
> 错误信息:
>   File "/usr/lib/python2.4/site-packages/MySQLdb/cursors.py", line 147, in execute
>     query = query.encode(charset)
> UnicodeDecodeError: 'ascii' codec can't decode byte 0xb4 in position 35: ordinal not in range(128)
>
>
> 各位高手,这个是为什么啊?
>
>
> 也试过把 MySQLdb的cursor。execute改成
> def execute(self, query, char="utf-8", args=None):
>         charset = char
>     query = query.encode(charset)
>
> 最终还是不行,数据库切切实实是utf8的啊。
>
> = = = = = = = = = = = = = = = = = = = =
>
>
> 致
> 礼!
>
>
>
>
> 李李
> 2008-01-21
>
>
> _______________________________________________
> python-chinese
> Post: send [hidden email]
> Subscribe: send subscribe to [hidden email]
> Unsubscribe: send unsubscribe to  [hidden email]
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
Reply | Threaded
Open this post in threaded view
|

Re: [求助]关于debian环境下通过MySQLdb查询数据库时UnicodeDecodeError的问题

leopay
In reply to this post by 弦灵风


2008/1/21 李李 <[hidden email]>:

最近在写些操作mysql数据的东西,发现写出来后windows下运行正常,但只要迁到debian环境下就无法查询带有中文的数据库了;

conn = MySQLdb.connect(host = host, db=db, user=user,
             passwd=passwd, use_unicode=1, charset='utf8')
cursor = conn.cursor ()

 

query = "select * from tab where su = '待'"
cursor.execute(query)

cursor.execute('select * from tab where su=%s','待')

row1=cursor.fetchall()

错误信息:
 File "/usr/lib/python2.4/site-packages/MySQLdb/cursors.py", line 147, in execute
   query = query.encode(charset)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xb4 in position 35: ordinal not in range(128)


各位高手,这个是为什么啊?


也试过把 MySQLdb的cursor。execute改成
def execute(self, query, char="utf-8", args=None):
       charset = char
   query = query.encode(charset)

最终还是不行,数据库切切实实是utf8的啊。

= = = = = = = = = = = = = = = = = = = =



礼!




李李
2008-01-21


_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese


_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese