Python论坛  - 讨论区

标题:[python-chinese] 请教一个编码问题

2007年07月13日 星期五 15:31

Airecho Python airecho.python在gmail.com
星期五 七月 13 15:31:48 HKT 2007

查询的代码如下:

#coding=gb2312
import sqlite3 as sqlite

def dbQuery():
    cx = sqlite.connect('test.db')
    cu = cx.cursor()

    cu.execute('select * from catalog')
    rs = cu.fetchall()
    print '>>>>>>>>>>>>>>>>>>>>>>>>>:',
    for data in rs:
        id,pid,name=data
        name=name.encode("gbk")
        print rs
        print id,pid,name

if __name__ == "__main__":
    dbQuery()

报错:

Traceback (most recent call last):
  File "C:\Documents and Settings\gbicc\workspace\pytest\src\dbQuery.py",
line 18, in 
    dbQuery()
  File "C:\Documents and Settings\gbicc\workspace\pytest\src\dbQuery.py",
line 8, in dbQuery
    cu.execute('select * from catalog')
sqlite3.OperationalError: Could not decode to UTF-8 column 'name' with text
'中国'

写入数据库的代码:

#coding=gbk
import sqlite3 as sqlite

def runTest():
    cx = sqlite.connect('test.db')
    cu = cx.cursor()

    #create
    cu.execute('''create table catalog(
        id integer primary key,
        pid integer,
        name varchar(10) unique
        )''')

    cu.execute('insert into catalog values(0,0,"中国")')
    cx.commit()

if __name__ == '__main__':
    runTest()

如果gbk改为utf-8,查询结果就是正确的,不知道为什么,请高手赐教。
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://python.cn/pipermail/python-chinese/attachments/20070713/4c9f58a1/attachment-0001.htm 

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

2007年07月13日 星期五 15:37

Xell Zhang xellzhang在gmail.com
星期五 七月 13 15:37:40 HKT 2007

ÒòΪÊý¾Ý¿âÀïÄǸö×ֶεıàÂëÊÇutf-8£¿

On 7/13/07, Airecho Python <airecho.python在gmail.com> wrote:
>
> ²éѯµÄ´úÂëÈçÏ£º
>
> #coding=gb2312
> import sqlite3 as sqlite
>
> def dbQuery():
>     cx = sqlite.connect('test.db')
>     cu = cx.cursor()
>
>     cu.execute('select * from catalog')
>     rs = cu.fetchall()
>     print '>>>>>>>>>>>>>>>>>>>>>>>>>:',
>     for data in rs:
>         id,pid,name=data
>         name=name.encode("gbk")
>         print rs
>         print id,pid,name
>
> if __name__ == "__main__":
>     dbQuery()
>
> ±¨´í£º
>
> Traceback (most recent call last):
>   File "C:\Documents and Settings\gbicc\workspace\pytest\src\dbQuery.py",
> line 18, in 
>     dbQuery()
>   File "C:\Documents and Settings\gbicc\workspace\pytest\src\dbQuery.py",
> line 8, in dbQuery
>     cu.execute('select * from catalog')
> sqlite3.OperationalError: Could not decode to UTF-8 column 'name' with
> text 'Öйú'
>
> дÈëÊý¾Ý¿âµÄ´úÂ룺
>
> #coding=gbk
> import sqlite3 as sqlite
>
> def runTest():
>     cx = sqlite.connect('test.db')
>     cu = cx.cursor()
>
>     #create
>     cu.execute('''create table catalog(
>         id integer primary key,
>         pid integer,
>         name varchar(10) unique
>         )''')
>
>     cu.execute('insert into catalog values(0,0,"Öйú")')
>     cx.commit()
>
> if __name__ == '__main__':
>     runTest()
>
> Èç¹ûgbk¸ÄΪutf-8£¬²éѯ½á¹û¾ÍÊÇÕýÈ·µÄ£¬²»ÖªµÀΪʲô£¬Çë¸ßÊִͽ̡£
>
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>



-- 
Zhang Xiao

Engineer, Web development

Ethos Tech.
http://www.ethos.com.cn
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070713/6be6c1cc/attachment.htm 

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

2007年07月13日 星期五 15:45

Airecho Python airecho.python在gmail.com
星期五 七月 13 15:45:03 HKT 2007

在07-7-13,Xell Zhang <xellzhang at gmail.com> 写道:
>
> 因为数据库里那个字段的编码是utf-8?


是的,但是这个为什么会导致查询出错呢?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://python.cn/pipermail/python-chinese/attachments/20070713/be57c44c/attachment.htm 

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

2007年07月13日 星期五 15:47

赵威 zhaoweikid在163.com
星期五 七月 13 15:47:06 HKT 2007

Airecho Python£¬ÄúºÃ£¡ 

¡¡¡¡sqlite3ĬÈϾÍÊÇÓõÄutf-8À´´æ´¢Êý¾ÝµÄ¡£

======== 2007-07-13 15:31:48 ÄúÔÚÀ´ÐÅÖÐдµÀ£º ========

²éѯµÄ´úÂëÈçÏ£º
#coding=gb2312
import sqlite3 as sqlite
def dbQuery():
    cx = sqlite.connect('test.db')
    cu = cx.cursor()
    
    cu.execute('select * from catalog')
    rs = cu.fetchall()
    print '>>>>>>>>>>>>>>>>>>>>>>>>>:', 
    for data in rs:
        id,pid,name=data
        name=name.encode("gbk")
        print rs
        print id,pid,name
if __name__ == "__main__":
    dbQuery()
±¨´í£º
Traceback (most recent call last):
  File "C:\Documents and Settings\gbicc\workspace\pytest\src\dbQuery.py", line 18, in 
    dbQuery()
  File "C:\Documents and Settings\gbicc\workspace\pytest\src\dbQuery.py", line 8, in dbQuery 
    cu.execute('select * from catalog')
sqlite3.OperationalError: Could not decode to UTF-8 column 'name' with text 'Öйú'
дÈëÊý¾Ý¿âµÄ´úÂ룺
#coding=gbk
import sqlite3 as sqlite
    
def runTest():
    cx = sqlite.connect('test.db')
    cu = cx.cursor()
    
    #create
    cu.execute('''create table catalog(
        id integer primary key,
        pid integer,
        name varchar(10) unique
        )''')
    cu.execute('insert into catalog values(0,0,"Öйú")')
    cx.commit()
if __name__ == '__main__':
    runTest()
Èç¹ûgbk¸ÄΪutf-8£¬²éѯ½á¹û¾ÍÊÇÕýÈ·µÄ£¬²»ÖªµÀΪʲô£¬Çë¸ßÊִͽ̡£

= = = = = = = = = = = = = = = = = = = = = = 
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ÖÂ
Àñ£¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ÕÔÍþ
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡zhaoweikid在163.com
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡2007-07-13
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070713/605baf9b/attachment.html 

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

2007年07月13日 星期五 15:50

Airecho Python airecho.python在gmail.com
星期五 七月 13 15:50:43 HKT 2007

在07-7-13,赵威 <zhaoweikid at 163.com> 写道:
>
> Airecho Python,您好!
>  sqlite3默认就是用的utf-8来存储数据的。
>

嗯,谢谢,但我还是不明白为什么会出错,在java里,就是出现类似"???"这样的乱码而已。
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://python.cn/pipermail/python-chinese/attachments/20070713/06380f2f/attachment.htm 

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

2007年07月13日 星期五 15:57

Xell Zhang xellzhang在gmail.com
星期五 七月 13 15:57:23 HKT 2007

²»Ì«Ã÷°×£¬ÄѵÀpythonÈÏΪ³öÏÖ???¾ÍÊÇÒ»¸öÒì³££¬ÓÚÊÇÔÚÕâ֮ǰÏÈÅжÏÁËÄܲ»ÄÜÕýÈ·µÄתÂ룿

On 7/13/07, Airecho Python <airecho.python在gmail.com> wrote:
>
>
>
> ÔÚ07-7-13£¬ÕÔÍþ <zhaoweikid在163.com> дµÀ£º
> >
> > Airecho Python£¬ÄúºÃ£¡
> >  sqlite3ĬÈϾÍÊÇÓõÄutf-8À´´æ´¢Êý¾ÝµÄ¡£
> >
>
> àÅ£¬Ð»Ð»£¬µ«ÎÒ»¹ÊDz»Ã÷°×Ϊʲô»á³ö´í£¬ÔÚjavaÀ¾ÍÊdzöÏÖÀàËÆ"£¿£¿£¿"ÕâÑùµÄÂÒÂë¶øÒÑ¡£
>
>
>
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>



-- 
Zhang Xiao

Engineer, Web development

Ethos Tech.
http://www.ethos.com.cn
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070713/0d51fccd/attachment-0001.html 

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

2007年07月14日 星期六 08:06

Tao Guan jack139在gmail.com
星期六 七月 14 08:06:02 HKT 2007

py文件也要用utf-8编码保存

在 07-7-13,Xell Zhang<xellzhang在gmail.com> 写道:
> 不太明白,难道python认为出现???就是一个异常,于是在这之前先判断了能不能正确的转码?
>
> On 7/13/07, Airecho Python <airecho.python在gmail.com> wrote:
> >
> >
> >
> >
> > 在07-7-13,赵威 <zhaoweikid在163.com> 写道:
> > >
> > > Airecho Python,您好!
> > >
> > >   sqlite3默认就是用的utf-8来存储数据的。
> >
> >
> > 嗯,谢谢,但我还是不明白为什么会出错,在java里,就是出现类似"???"这样的乱码而已。
> >
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese在lists.python.cn
> > Subscribe: send subscribe to
> python-chinese-request在lists.python.cn
> > Unsubscribe: send unsubscribe to
> python-chinese-request在lists.python.cn
> > Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
> >
>
>
>
> --
> Zhang Xiao
>
> Engineer, Web development
>
> Ethos Tech.
> http://www.ethos.com.cn
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to
> python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to
> python-chinese-request在lists.python.cn
> Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
>

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

2007年07月14日 星期六 10:20

limodou limodou在gmail.com
星期六 七月 14 10:20:49 HKT 2007

On 7/13/07, Airecho Python <airecho.python在gmail.com> wrote:
>
>
>
> 在07-7-13,赵威 <zhaoweikid在163.com> 写道:
> >
> > Airecho Python,您好!
> >
> >   sqlite3默认就是用的utf-8来存储数据的。
>
>
> 嗯,谢谢,但我还是不明白为什么会出错,在java里,就是出现类似"???"这样的乱码而已。
>
sqlite3是使用unicode来保存数据的,因此如果你插入时给出的不是unicode,它会自动转换。而当转换出错时,会使用?来替换,这是一种标准的unicode转换出错时的处理。具体的可以参数unicode函数的说明。

-- 
I like python!
UliPad <>: http://wiki.woodpecker.org.cn/moin/UliPad
My Blog: http://www.donews.net/limodou

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

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

    你的回复:

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

    Zeuux © 2025

    京ICP备05028076号