Python论坛  - 讨论区

标题:[python-chinese] MySQL

2006年08月09日 星期三 11:16

R.Potato rough.vanzolo at gmail.com
Wed Aug 9 11:16:57 HKT 2006

Hi All,

我在用python建一个简单的数据库应用。其中需要向数据库里面插入数据,但是在插入中文数据的时候总是出现错误。

Traceback (most recent call last):
  File "C:\cis\cgi-bin\cisdb.py", line 247, in test12
    [(2, 'router'), (4, 'modem'), (1, 'trans1')])
  File "C:\cis\cgi-bin\cisdb.py", line 58, in wrapper
    ret = func(*args, **kwargs)
  File "C:\cis\cgi-bin\cisdb.py", line 140, in add_client
    cid = self._add_client_info(name, contact, phone, other_access,
recourse, misc, topology)
  File "C:\cis\cgi-bin\cisdb.py", line 147, in _add_client_info
    self.cursor.execute("insert into clients (name, contact, phone,
other_access, recourse, misc, topology) "\
  File "C:\Python24\Lib\site-packages\MySQLdb\cursors.py", line 146, in
execute
    query = query.encode(charset)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 92:
ordinal not in range(128)
MySQL版本是5.0,使用utf8作为default character set。MySQLdb版本是4.1.0,python版本是2.4.3
我使用下面的代码建立连接:

            self.connection = MySQLdb.connect(host = self.database_hostname,
user = self.username,
                                              passwd = self.password, db =
self.database_name,
                                              charset = self.charset,
use_unicode = False)
其中self.charset='utf8'。
但是在跟踪的时候我发现在cursor里面的execute()中connection的charset并不是'utf8'而是'ascii'。
        ....
        db = self._get_db()
        charset = db.character_set_name()
        ....
这应该就是出现问题的原因。但我不明白为什么在connect()里面设置的charset没有起作用。没有办法跟踪进_mysql,也就无从知道connection的character_set_name()里面都发生了什么。有没有什么办法可以解决这个问题?谢谢啦

-- 
Thanks&&Regards;,
Li Hongliang

挟一卷书
观天测地
仗一口剑
保家卫国
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060809/83383ee5/attachment.htm

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年08月09日 星期三 11:20

刘鑫 march.liu at gmail.com
Wed Aug 9 11:20:26 HKT 2006

self.cursor.execute(u"insert into clients (name, contact, phone,
other_access, recourse, misc, topology) ".encod('uft-8'))
试试看吧,这个库我没用过,只是在其它场合遇到类似问题的一点经验。

在06-8-9,R. Potato <rough.vanzolo at gmail.com> 写道:
>
> Hi All,
>
> 我在用python建一个简单的数据库应用。其中需要向数据库里面插入数据,但是在插入中文数据的时候总是出现错误。
>
> Traceback (most recent call last):
>   File "C:\cis\cgi-bin\cisdb.py", line 247, in test12
>     [(2, 'router'), (4, 'modem'), (1, 'trans1')])
>   File "C:\cis\cgi-bin\cisdb.py", line 58, in wrapper
>     ret = func(*args, **kwargs)
>   File "C:\cis\cgi-bin\cisdb.py", line 140, in add_client
>     cid = self._add_client_info(name, contact, phone, other_access,
> recourse, misc, topology)
>   File "C:\cis\cgi-bin\cisdb.py", line 147, in _add_client_info
>     self.cursor.execute("insert into clients (name, contact, phone,
> other_access, recourse, misc, topology) "\
>   File "C:\Python24\Lib\site-packages\MySQLdb\cursors.py", line 146, in
> execute
>     query = query.encode(charset)
> UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 92:
> ordinal not in range(128)
> MySQL版本是5.0,使用utf8作为default character set。MySQLdb版本是4.1.0,python版本是2.4.3
> 我使用下面的代码建立连接:
>
>             self.connection = MySQLdb.connect(host =
> self.database_hostname, user = self.username,
>                                               passwd = self.password, db =
> self.database_name ,
>                                               charset = self.charset,
> use_unicode = False)
> 其中self.charset='utf8'。
> 但是在跟踪的时候我发现在cursor里面的execute()中connection的charset并不是'utf8'而是'ascii'。
>         ....
>         db = self._get_db()
>         charset = db.character_set_name()
>         ....
>
> 这应该就是出现问题的原因。但我不明白为什么在connect()里面设置的charset没有起作用。没有办法跟踪进_mysql,也就无从知道connection的character_set_name()里面都发生了什么。有没有什么办法可以解决这个问题?谢谢啦
>
> --
> Thanks&&Regards;,
> Li Hongliang
>
> 挟一卷书
> 观天测地
> 仗一口剑
> 保家卫国
>
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>


-- 
欢迎访问:
http://blog.csdn.net/ccat

刘鑫
March.Liu
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060809/1ed9f211/attachment.html

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年08月09日 星期三 11:31

R.Potato rough.vanzolo at gmail.com
Wed Aug 9 11:31:19 HKT 2006

我写了一个最简单的测试函数:
        self.cursor.execute("insert into clients (name) values
('客户')".encode('utf-8'))
但是错误还是有
Traceback (most recent call last):
  File "C:\cis\cgi-bin\cisdb.py", line 249, in test12
    db.test()
  File "C:\cis\cgi-bin\cisdb.py", line 58, in wrapper
    ret = func(*args, **kwargs)
  File "C:\cis\cgi-bin\cisdb.py", line 201, in test
    self.cursor.execute("insert into clients (name) values
('客户')".encode('utf-8'))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xbf in position 36:
ordinal not in range(128)



On 8/9/06, 刘鑫 <march.liu at gmail.com> wrote:
>
> self.cursor.execute(u"insert into clients (name, contact, phone,
> other_access, recourse, misc, topology) ".encod('uft-8'))
> 试试看吧,这个库我没用过,只是在其它场合遇到类似问题的一点经验。
>
> 在06-8-9,R. Potato <rough.vanzolo at gmail.com > 写道:
> >
> >  Hi All,
> >
> > 我在用python建一个简单的数据库应用。其中需要向数据库里面插入数据,但是在插入中文数据的时候总是出现错误。
> >
> > Traceback (most recent call last):
> >   File "C:\cis\cgi-bin\cisdb.py", line 247, in test12
> >     [(2, 'router'), (4, 'modem'), (1, 'trans1')])
> >   File "C:\cis\cgi-bin\cisdb.py", line 58, in wrapper
> >     ret = func(*args, **kwargs)
> >   File "C:\cis\cgi-bin\cisdb.py", line 140, in add_client
> >     cid = self._add_client_info(name, contact, phone, other_access,
> > recourse, misc, topology)
> >   File "C:\cis\cgi-bin\cisdb.py", line 147, in _add_client_info
> >     self.cursor.execute("insert into clients (name, contact, phone,
> > other_access, recourse, misc, topology) "\
> >   File "C:\Python24\Lib\site-packages\MySQLdb\cursors.py", line 146, in
> > execute
> >     query = query.encode(charset)
> > UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 92:
> > ordinal not in range(128)
> > MySQL版本是5.0,使用utf8作为default character set。MySQLdb版本是4.1.0,python版本是2.4.3
> > 我使用下面的代码建立连接:
> >
> >             self.connection = MySQLdb.connect(host =
> > self.database_hostname, user = self.username,
> >                                               passwd = self.password, db
> > = self.database_name ,
> >                                               charset = self.charset,
> > use_unicode = False)
> > 其中self.charset='utf8'。
> > 但是在跟踪的时候我发现在cursor里面的execute()中connection的charset并不是'utf8'而是'ascii'。
> >         ....
> >         db = self._get_db()
> >         charset = db.character_set_name()
> >         ....
> >
> > 这应该就是出现问题的原因。但我不明白为什么在connect()里面设置的charset没有起作用。没有办法跟踪进_mysql,也就无从知道connection的character_set_name()里面都发生了什么。有没有什么办法可以解决这个问题?谢谢啦
> >
> > --
> > Thanks&&Regards;,
> > Li Hongliang
> >
> > 挟一卷书
> > 观天测地
> > 仗一口剑
> > 保家卫国
> >
> >
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese at lists.python.cn
> > Subscribe: send subscribe to python-chinese-request at lists.python.cn
> > Unsubscribe: send unsubscribe to
> > python-chinese-request at lists.python.cn
> > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> >
> >
>
>
> --
> 欢迎访问:
> http://blog.csdn.net/ccat
>
> 刘鑫
> March.Liu
>
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>


-- 
Thanks&&Regards;,
Li Hongliang

挟一卷书
观天测地
仗一口剑
保家卫国
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060809/ef31f032/attachment.htm

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年08月09日 星期三 11:43

lu lubiao.py at gmail.com
Wed Aug 9 11:43:15 HKT 2006

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
开头加上这个

并且文件 按 utf8 编码保存

On 8/9/06, R. Potato <rough.vanzolo at gmail.com> wrote:
>
>  我写了一个最简单的测试函数:
>         self.cursor.execute("insert into clients (name) values
> ('客户')".encode('utf-8'))
> 但是错误还是有
>  Traceback (most recent call last):
>   File "C:\cis\cgi-bin\cisdb.py", line 249, in test12
>     db.test()
>
>   File "C:\cis\cgi-bin\cisdb.py", line 58, in wrapper
>     ret = func(*args, **kwargs)
>   File "C:\cis\cgi-bin\cisdb.py", line 201, in test
>     self.cursor.execute("insert into clients (name) values
> ('客户')".encode('utf-8'))
> UnicodeDecodeError: 'ascii' codec can't decode byte 0xbf in position 36:
> ordinal not in range(128)
>
>
>
>  On 8/9/06, 刘鑫 <march.liu at gmail.com> wrote:
> >
> > self.cursor.execute(u"insert into clients (name, contact, phone,
> > other_access, recourse, misc, topology) ".encod('uft-8'))
> > 试试看吧,这个库我没用过,只是在其它场合遇到类似问题的一点经验。
> >
> > 在06-8-9,R. Potato <rough.vanzolo at gmail.com > 写道:
> > >
> > >  Hi All,
> > >
> > > 我在用python建一个简单的数据库应用。其中需要向数据库里面插入数据,但是在插入中文数据的时候总是出现错误。
> > >
> > > Traceback (most recent call last):
> > >   File "C:\cis\cgi-bin\cisdb.py", line 247, in test12
> > >     [(2, 'router'), (4, 'modem'), (1, 'trans1')])
> > >   File "C:\cis\cgi-bin\cisdb.py", line 58, in wrapper
> > >     ret = func(*args, **kwargs)
> > >   File "C:\cis\cgi-bin\cisdb.py", line 140, in add_client
> > >     cid = self._add_client_info(name, contact, phone, other_access,
> > > recourse, misc, topology)
> > >   File "C:\cis\cgi-bin\cisdb.py", line 147, in _add_client_info
> > >     self.cursor.execute("insert into clients (name, contact, phone,
> > > other_access, recourse, misc, topology) "\
> > >   File "C:\Python24\Lib\site-packages\MySQLdb\cursors.py", line 146,
> > > in execute
> > >     query = query.encode(charset)
> > > UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position
> > > 92: ordinal not in range(128)
> > > MySQL版本是5.0,使用utf8作为default character set。MySQLdb版本是4.1.0,python
> > > 版本是2.4.3
> > > 我使用下面的代码建立连接:
> > >
> > >             self.connection = MySQLdb.connect(host =
> > > self.database_hostname, user = self.username,
> > >                                               passwd = self.password,
> > > db = self.database_name ,
> > >                                               charset = self.charset,
> > > use_unicode = False)
> > > 其中self.charset='utf8'。
> > > 但是在跟踪的时候我发现在cursor里面的execute()中connection的charset并不是'utf8'而是'ascii'。
> > >         ....
> > >         db = self._get_db()
> > >         charset = db.character_set_name()
> > >         ....
> > >
> > > 这应该就是出现问题的原因。但我不明白为什么在connect()里面设置的charset没有起作用。没有办法跟踪进_mysql,也就无从知道connection的character_set_name()里面都发生了什么。有没有什么办法可以解决这个问题?谢谢啦
> > >
> > > --
> > > Thanks&&Regards;,
> > > Li Hongliang
> > >
> > > 挟一卷书
> > > 观天测地
> > > 仗一口剑
> > > 保家卫国
> > >
> > >
> > > _______________________________________________
> > > python-chinese
> > > Post: send python-chinese at lists.python.cn
> > > Subscribe: send subscribe to python-chinese-request at lists.python.cn
> > > Unsubscribe: send unsubscribe to
> > > python-chinese-request at lists.python.cn
> > > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> > >
> > >
> >
> >
> > --
> > 欢迎访问:
> > http://blog.csdn.net/ccat
> >
> > 刘鑫
> > March.Liu
> >
> >
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese at lists.python.cn
> > Subscribe: send subscribe to python-chinese-request at lists.python.cn
> > Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> >
> >
>
>
> --
> Thanks&&Regards;,
> Li Hongliang
>
> 挟一卷书
> 观天测地
> 仗一口剑
> 保家卫国
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060809/94dcfa7b/attachment.htm

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年08月09日 星期三 12:15

R.Potato rough.vanzolo at gmail.com
Wed Aug 9 12:15:58 HKT 2006

在windows下面默认应该是使用gbk编码的。在文件开始加上
# -*- coding: gbk -*-
就可以使用u'XXXXX'这种字符串了,不一定是要用utf-8的coding加上utf8编码的文件。
但是我这里没有涉及到使用unicode字符串,而是使用encode(),应该没有这个问题。我也按照上面说的做了,结果还是一样有错。


On 8/9/06, lu <lubiao.py at gmail.com> wrote:
>
>  #!/usr/bin/env python
> # -*- coding: UTF-8 -*-
> 开头加上这个
>
> 并且文件 按 utf8 编码保存
>
>  On 8/9/06, R. Potato <rough.vanzolo at gmail.com> wrote:
> >
> >  我写了一个最简单的测试函数:
> >         self.cursor.execute("insert into clients (name) values
> > ('客户')".encode('utf-8'))
> > 但是错误还是有
> >  Traceback (most recent call last):
> >   File "C:\cis\cgi-bin\cisdb.py", line 249, in test12
> >     db.test()
> >
> >   File "C:\cis\cgi-bin\cisdb.py", line 58, in wrapper
> >     ret = func(*args, **kwargs)
> >   File "C:\cis\cgi-bin\cisdb.py", line 201, in test
> >     self.cursor.execute("insert into clients (name) values
> > ('客户')".encode('utf-8'))
> > UnicodeDecodeError: 'ascii' codec can't decode byte 0xbf in position 36:
> > ordinal not in range(128)
> >
> >
> >
> >  On 8/9/06, 刘鑫 <march.liu at gmail.com> wrote:
> > >
> > > self.cursor.execute(u"insert into clients (name, contact, phone,
> > > other_access, recourse, misc, topology) ".encod('uft-8'))
> > > 试试看吧,这个库我没用过,只是在其它场合遇到类似问题的一点经验。
> > >
> > > 在06-8-9,R. Potato <rough.vanzolo at gmail.com > 写道:
> > > >
> > > >  Hi All,
> > > >
> > > > 我在用python建一个简单的数据库应用。其中需要向数据库里面插入数据,但是在插入中文数据的时候总是出现错误。
> > > >
> > > > Traceback (most recent call last):
> > > >   File "C:\cis\cgi-bin\cisdb.py", line 247, in test12
> > > >     [(2, 'router'), (4, 'modem'), (1, 'trans1')])
> > > >   File "C:\cis\cgi-bin\cisdb.py", line 58, in wrapper
> > > >     ret = func(*args, **kwargs)
> > > >   File "C:\cis\cgi-bin\cisdb.py", line 140, in add_client
> > > >     cid = self._add_client_info(name, contact, phone, other_access,
> > > > recourse, misc, topology)
> > > >   File "C:\cis\cgi-bin\cisdb.py", line 147, in _add_client_info
> > > >     self.cursor.execute("insert into clients (name, contact, phone,
> > > > other_access, recourse, misc, topology) "\
> > > >   File "C:\Python24\Lib\site-packages\MySQLdb\cursors.py", line 146,
> > > > in execute
> > > >     query = query.encode(charset)
> > > > UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position
> > > > 92: ordinal not in range(128)
> > > > MySQL版本是5.0,使用utf8作为default character set。MySQLdb版本是4.1.0,python
> > > > 版本是2.4.3
> > > > 我使用下面的代码建立连接:
> > > >
> > > >             self.connection = MySQLdb.connect(host =
> > > > self.database_hostname, user = self.username,
> > > >                                               passwd = self.password,
> > > > db = self.database_name ,
> > > >                                               charset = self.charset,
> > > > use_unicode = False)
> > > > 其中self.charset='utf8'。
> > > > 但是在跟踪的时候我发现在cursor里面的execute()中connection的charset并不是'utf8'而是'ascii'。
> > > >         ....
> > > >         db = self._get_db()
> > > >         charset = db.character_set_name()
> > > >         ....
> > > >
> > > > 这应该就是出现问题的原因。但我不明白为什么在connect()里面设置的charset没有起作用。没有办法跟踪进_mysql,也就无从知道connection的character_set_name()里面都发生了什么。有没有什么办法可以解决这个问题?谢谢啦
> > > >
> > > > --
> > > > Thanks&&Regards;,
> > > > Li Hongliang
> > > >
> > > > 挟一卷书
> > > > 观天测地
> > > > 仗一口剑
> > > > 保家卫国
> > > >
> > > >
> > > > _______________________________________________
> > > > python-chinese
> > > > Post: send python-chinese at lists.python.cn
> > > > Subscribe: send subscribe to python-chinese-request at lists.python.cn
> > > > Unsubscribe: send unsubscribe to
> > > > python-chinese-request at lists.python.cn
> > > > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> > > >
> > > >
> > >
> > >
> > > --
> > > 欢迎访问:
> > > http://blog.csdn.net/ccat
> > >
> > > 刘鑫
> > > March.Liu
> > >
> > >
> > > _______________________________________________
> > > python-chinese
> > > Post: send python-chinese at lists.python.cn
> > > Subscribe: send subscribe to python-chinese-request at lists.python.cn
> > > Unsubscribe: send unsubscribe to
> > > python-chinese-request at lists.python.cn
> > > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> > >
> > >
> >
> >
> > --
> > Thanks&&Regards;,
> > Li Hongliang
> >
> > 挟一卷书
> > 观天测地
> > 仗一口剑
> > 保家卫国
> >
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese at lists.python.cn
> > Subscribe: send subscribe to python-chinese-request at lists.python.cn
> > Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> >
> >
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>


-- 
Thanks&&Regards;,
Li Hongliang

挟一卷书
观天测地
仗一口剑
保家卫国
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060809/8a31fb1c/attachment.htm

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年08月09日 星期三 12:34

刘鑫 march.liu at gmail.com
Wed Aug 9 12:34:31 HKT 2006

是u'string'.encod('utf-8'),确实应该在前边加utf-8编码标记,不知道能否对您有帮助,祝好运。

在06-8-9,R. Potato <rough.vanzolo at gmail.com> 写道:
>
> 在windows下面默认应该是使用gbk编码的。在文件开始加上
> # -*- coding: gbk -*-
> 就可以使用u'XXXXX'这种字符串了,不一定是要用utf-8的coding加上utf8编码的文件。
> 但是我这里没有涉及到使用unicode字符串,而是使用encode(),应该没有这个问题。我也按照上面说的做了,结果还是一样有错。
>
>
> On 8/9/06, lu <lubiao.py at gmail.com> wrote:
> >
> >  #!/usr/bin/env python
> > # -*- coding: UTF-8 -*-
> > 开头加上这个
> >
> > 并且文件 按 utf8 编码保存
> >
> >  On 8/9/06, R. Potato <rough.vanzolo at gmail.com > wrote:
> > >
> > >  我写了一个最简单的测试函数:
> > >         self.cursor.execute("insert into clients (name) values
> > > ('客户')".encode('utf-8'))
> > > 但是错误还是有
> > >  Traceback (most recent call last):
> > >   File "C:\cis\cgi-bin\cisdb.py", line 249, in test12
> > >     db.test()
> > >
> > >   File "C:\cis\cgi-bin\cisdb.py", line 58, in wrapper
> > >     ret = func(*args, **kwargs)
> > >   File "C:\cis\cgi-bin\cisdb.py", line 201, in test
> > >     self.cursor.execute("insert into clients (name) values
> > > ('客户')".encode('utf-8'))
> > > UnicodeDecodeError: 'ascii' codec can't decode byte 0xbf in position
> > > 36: ordinal not in range(128)
> > >
> > >
> > >
> > >  On 8/9/06, 刘鑫 <march.liu at gmail.com> wrote:
> > > >
> > > > self.cursor.execute(u"insert into clients (name, contact, phone,
> > > > other_access, recourse, misc, topology) ".encod('uft-8'))
> > > > 试试看吧,这个库我没用过,只是在其它场合遇到类似问题的一点经验。
> > > >
> > > > 在06-8-9,R. Potato <rough.vanzolo at gmail.com > 写道:
> > > > >
> > > > >  Hi All,
> > > > >
> > > > > 我在用python建一个简单的数据库应用。其中需要向数据库里面插入数据,但是在插入中文数据的时候总是出现错误。
> > > > >
> > > > > Traceback (most recent call last):
> > > > >   File "C:\cis\cgi-bin\cisdb.py", line 247, in test12
> > > > >     [(2, 'router'), (4, 'modem'), (1, 'trans1')])
> > > > >   File "C:\cis\cgi-bin\cisdb.py", line 58, in wrapper
> > > > >     ret = func(*args, **kwargs)
> > > > >   File "C:\cis\cgi-bin\cisdb.py", line 140, in add_client
> > > > >     cid = self._add_client_info(name, contact, phone,
> > > > > other_access, recourse, misc, topology)
> > > > >   File "C:\cis\cgi-bin\cisdb.py", line 147, in _add_client_info
> > > > >     self.cursor.execute("insert into clients (name, contact,
> > > > > phone, other_access, recourse, misc, topology) "\
> > > > >   File "C:\Python24\Lib\site-packages\MySQLdb\cursors.py", line
> > > > > 146, in execute
> > > > >     query = query.encode(charset)
> > > > > UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in
> > > > > position 92: ordinal not in range(128)
> > > > > MySQL版本是5.0,使用utf8作为default character set。MySQLdb版本是4.1.0,python
> > > > > 版本是2.4.3
> > > > > 我使用下面的代码建立连接:
> > > > >
> > > > >             self.connection = MySQLdb.connect(host =
> > > > > self.database_hostname, user = self.username,
> > > > >                                               passwd =
> > > > > self.password, db = self.database_name ,
> > > > >                                               charset =
> > > > > self.charset, use_unicode = False)
> > > > > 其中self.charset='utf8'。
> > > > >
> > > > > 但是在跟踪的时候我发现在cursor里面的execute()中connection的charset并不是'utf8'而是'ascii'。
> > > > >         ....
> > > > >         db = self._get_db()
> > > > >         charset = db.character_set_name()
> > > > >         ....
> > > > >
> > > > > 这应该就是出现问题的原因。但我不明白为什么在connect()里面设置的charset没有起作用。没有办法跟踪进_mysql,也就无从知道connection的character_set_name()里面都发生了什么。有没有什么办法可以解决这个问题?谢谢啦
> > > > >
> > > > > --
> > > > > Thanks&&Regards;,
> > > > > Li Hongliang
> > > > >
> > > > > 挟一卷书
> > > > > 观天测地
> > > > > 仗一口剑
> > > > > 保家卫国
> > > > >
> > > > >
> > > > > _______________________________________________
> > > > > python-chinese
> > > > > Post: send python-chinese at lists.python.cn
> > > > > Subscribe: send subscribe to
> > > > > python-chinese-request at lists.python.cn
> > > > > Unsubscribe: send unsubscribe to
> > > > > python-chinese-request at lists.python.cn
> > > > > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> > > > >
> > > > >
> > > >
> > > >
> > > > --
> > > > 欢迎访问:
> > > > http://blog.csdn.net/ccat
> > > >
> > > > 刘鑫
> > > > March.Liu
> > > >
> > > >
> > > > _______________________________________________
> > > > python-chinese
> > > > Post: send python-chinese at lists.python.cn
> > > > Subscribe: send subscribe to python-chinese-request at lists.python.cn
> > > > Unsubscribe: send unsubscribe to
> > > > python-chinese-request at lists.python.cn
> > > > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> > > >
> > > >
> > >
> > >
> > > --
> > > Thanks&&Regards;,
> > > Li Hongliang
> > >
> > > 挟一卷书
> > > 观天测地
> > > 仗一口剑
> > > 保家卫国
> > >
> > > _______________________________________________
> > > python-chinese
> > > Post: send python-chinese at lists.python.cn
> > > Subscribe: send subscribe to python-chinese-request at lists.python.cn
> > > Unsubscribe: send unsubscribe to
> > > python-chinese-request at lists.python.cn
> > > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> > >
> > >
> >
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese at lists.python.cn
> > Subscribe: send subscribe to python-chinese-request at lists.python.cn
> > Unsubscribe: send unsubscribe to
> > python-chinese-request at lists.python.cn
> > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> >
> >
>
>
> --
> Thanks&&Regards;,
> Li Hongliang
>
> 挟一卷书
> 观天测地
> 仗一口剑
> 保家卫国
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>


-- 
欢迎访问:
http://blog.csdn.net/ccat

刘鑫
March.Liu
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060809/24b247d8/attachment.html

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年08月09日 星期三 17:05

wang yingqi wangyingqi at gmail.com
Wed Aug 9 17:05:59 HKT 2006

有中文字符的话,用这样的方法传递,execute有一个可以传递第二个参数的方法,将查询字符串中的%S替换为该参数。试验一下看!

   def get_items(self, keyword = ''):
        query = "SELECT * FROM items WHERE name LIKE %s;"
        cursor = self.db.cursor()
        keyword = keyword.encode()
        keyword = '%' + keyword + '%'
        fetchcnt = cursor.execute(query, keyword)





On 8/9/06, 刘鑫 <march.liu at gmail.com> wrote:
>
> 是u'string'.encod('utf-8'),确实应该在前边加utf-8编码标记,不知道能否对您有帮助,祝好运。
>
>
> 在06-8-9,R. Potato < rough.vanzolo at gmail.com> 写道:
> >
> >  在windows下面默认应该是使用gbk编码的。在文件开始加上
> > # -*- coding: gbk -*-
> > 就可以使用u'XXXXX'这种字符串了,不一定是要用utf-8的coding加上utf8编码的文件。
> > 但是我这里没有涉及到使用unicode字符串,而是使用encode(),应该没有这个问题。我也按照上面说的做了,结果还是一样有错。
> >
> >
> >  On 8/9/06, lu <lubiao.py at gmail.com> wrote:
> > >
> > >  #!/usr/bin/env python
> > > # -*- coding: UTF-8 -*-
> > > 开头加上这个
> > >
> > > 并且文件 按 utf8 编码保存
> > >
> > >  On 8/9/06, R. Potato <rough.vanzolo at gmail.com > wrote:
> > > >
> > > >  我写了一个最简单的测试函数:
> > > >         self.cursor.execute("insert into clients (name) values
> > > > ('客户')".encode('utf-8'))
> > > > 但是错误还是有
> > > >  Traceback (most recent call last):
> > > >   File "C:\cis\cgi-bin\cisdb.py", line 249, in test12
> > > >     db.test()
> > > >
> > > >   File "C:\cis\cgi-bin\cisdb.py", line 58, in wrapper
> > > >     ret = func(*args, **kwargs)
> > > >   File "C:\cis\cgi-bin\cisdb.py", line 201, in test
> > > >     self.cursor.execute("insert into clients (name) values
> > > > ('客户')".encode('utf-8'))
> > > > UnicodeDecodeError: 'ascii' codec can't decode byte 0xbf in position
> > > > 36: ordinal not in range(128)
> > > >
> > > >
> > > >
> > > >  On 8/9/06, 刘鑫 <march.liu at gmail.com> wrote:
> > > > >
> > > > > self.cursor.execute(u"insert into clients (name, contact, phone,
> > > > > other_access, recourse, misc, topology) ".encod('uft-8'))
> > > > > 试试看吧,这个库我没用过,只是在其它场合遇到类似问题的一点经验。
> > > > >
> > > > > 在06-8-9,R. Potato <rough.vanzolo at gmail.com > 写道:
> > > > > >
> > > > > >  Hi All,
> > > > > >
> > > > > > 我在用python建一个简单的数据库应用。其中需要向数据库里面插入数据,但是在插入中文数据的时候总是出现错误。
> > > > > >
> > > > > > Traceback (most recent call last):
> > > > > >   File "C:\cis\cgi-bin\cisdb.py", line 247, in test12
> > > > > >     [(2, 'router'), (4, 'modem'), (1, 'trans1')])
> > > > > >   File "C:\cis\cgi-bin\cisdb.py", line 58, in wrapper
> > > > > >     ret = func(*args, **kwargs)
> > > > > >   File "C:\cis\cgi-bin\cisdb.py", line 140, in add_client
> > > > > >     cid = self._add_client_info(name, contact, phone,
> > > > > > other_access, recourse, misc, topology)
> > > > > >   File "C:\cis\cgi-bin\cisdb.py", line 147, in _add_client_info
> > > > > >     self.cursor.execute("insert into clients (name, contact,
> > > > > > phone, other_access, recourse, misc, topology) "\
> > > > > >   File "C:\Python24\Lib\site-packages\MySQLdb\cursors.py", line
> > > > > > 146, in execute
> > > > > >     query = query.encode(charset)
> > > > > > UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in
> > > > > > position 92: ordinal not in range(128)
> > > > > > MySQL版本是5.0,使用utf8作为default character set。MySQLdb版本是4.1.0,python
> > > > > > 版本是2.4.3
> > > > > > 我使用下面的代码建立连接:
> > > > > >
> > > > > >             self.connection = MySQLdb.connect(host =
> > > > > > self.database_hostname, user = self.username,
> > > > > >                                               passwd =
> > > > > > self.password, db = self.database_name ,
> > > > > >                                               charset =
> > > > > > self.charset, use_unicode = False)
> > > > > > 其中self.charset='utf8'。
> > > > > >
> > > > > > 但是在跟踪的时候我发现在cursor里面的execute()中connection的charset并不是'utf8'而是'ascii'。
> > > > > >         ....
> > > > > >         db = self._get_db()
> > > > > >         charset = db.character_set_name()
> > > > > >         ....
> > > > > >
> > > > > > 这应该就是出现问题的原因。但我不明白为什么在connect()里面设置的charset没有起作用。没有办法跟踪进_mysql,也就无从知道connection的character_set_name()里面都发生了什么。有没有什么办法可以解决这个问题?谢谢啦
> > > > > >
> > > > > > --
> > > > > > Thanks&&Regards;,
> > > > > > Li Hongliang
> > > > > >
> > > > > > 挟一卷书
> > > > > > 观天测地
> > > > > > 仗一口剑
> > > > > > 保家卫国
> > > > > >
> > > > > >
> > > > > > _______________________________________________
> > > > > > python-chinese
> > > > > > Post: send python-chinese at lists.python.cn
> > > > > > Subscribe: send subscribe to
> > > > > > python-chinese-request at lists.python.cn
> > > > > > Unsubscribe: send unsubscribe to
> > > > > > python-chinese-request at lists.python.cn
> > > > > > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> > > > > >
> > > > > >
> > > > >
> > > > >
> > > > > --
> > > > > 欢迎访问:
> > > > > http://blog.csdn.net/ccat
> > > > >
> > > > > 刘鑫
> > > > > March.Liu
> > > > >
> > > > >
> > > > > _______________________________________________
> > > > > python-chinese
> > > > > Post: send python-chinese at lists.python.cn
> > > > > Subscribe: send subscribe to python-chinese-request at lists.python.cn
> > > > >
> > > > > Unsubscribe: send unsubscribe to
> > > > > python-chinese-request at lists.python.cn
> > > > > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> > > > >
> > > > >
> > > >
> > > >
> > > > --
> > > > Thanks&&Regards;,
> > > > Li Hongliang
> > > >
> > > > 挟一卷书
> > > > 观天测地
> > > > 仗一口剑
> > > > 保家卫国
> > > >
> > > > _______________________________________________
> > > > python-chinese
> > > > Post: send python-chinese at lists.python.cn
> > > > Subscribe: send subscribe to python-chinese-request at lists.python.cn
> > > > Unsubscribe: send unsubscribe to
> > > > python-chinese-request at lists.python.cn
> > > > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> > > >
> > > >
> > >
> > > _______________________________________________
> > > python-chinese
> > > Post: send python-chinese at lists.python.cn
> > > Subscribe: send subscribe to python-chinese-request at lists.python.cn
> > > Unsubscribe: send unsubscribe to
> > > python-chinese-request at lists.python.cn
> > > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> > >
> > >
> >
> >
> > --
> > Thanks&&Regards;,
> > Li Hongliang
> >
> > 挟一卷书
> > 观天测地
> > 仗一口剑
> > 保家卫国
> >
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese at lists.python.cn
> > Subscribe: send subscribe to python-chinese-request at lists.python.cn
> > Unsubscribe: send unsubscribe to
> > python-chinese-request at lists.python.cn
> > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> >
> >
>
>
> --
> 欢迎访问:
> http://blog.csdn.net/ccat
>
> 刘鑫
> March.Liu
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>


-- 
wangyingqi
c++/python/palm/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060809/e0c6bcf6/attachment.html

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号