sqlite.connect 不支持gbk中文...

14 views
Skip to first unread message

chunlin zhang

unread,
Sep 6, 2007, 11:48:33 PM9/6/07
to pyth...@googlegroups.com
unicode是不行的...
self.string_standard_db_connect = sqlite.connect(filepath)
:
self.string_standard_db_connect = sqlite.connect(filepath)
UnicodeEncodeError: 'ascii' codec can't encode characters in position
34-35: ordinal not in range(128)

------------------------------------------
gbk也不行...

self.string_standard_db_connect = sqlite.connect(filepath.encode('cp936'))
:
pysqlite2.dbapi2.OperationalError: unable to open database file

limodou

unread,
Sep 7, 2007, 12:02:27 AM9/7/07
to pyth...@googlegroups.com
数据库文件名还用了中文?使用unicode试试.

--
I like python!
UliPad <<The Python Editor>>: http://code.google.com/p/ulipad/
My Blog: http://www.donews.net/limodou

chunlin

unread,
Sep 7, 2007, 12:56:04 AM9/7/07
to python.cn
不是 ,我 我的程序拷到 面上就不能 行了
因 面的路径有中文.

数据 文件名当然不会起个中文名.

filepath 就是 unicode
filepath.encode('cp936') 个 是gbk

所以 都不行.

On 9月7日, 下午12 02分, limodou <limo...@gmail.com> wrote:


> On 9/7/07, chunlin zhang <zhangchun...@gmail.com> wrote:
>
> > unicode是不行的...
> > self.string_standard_db_connect = sqlite.connect(filepath)
> > :
> > self.string_standard_db_connect = sqlite.connect(filepath)
> > UnicodeEncodeError: 'ascii' codec can't encode characters in position
> > 34-35: ordinal not in range(128)
>
> > ------------------------------------------
> > gbk也不行...
>
> > self.string_standard_db_connect = sqlite.connect(filepath.encode('cp936'))
> > :
> > pysqlite2.dbapi2.OperationalError: unable to open database file
>

> 数据 文件名 用了中文?使用unicode .

limodou

unread,
Sep 7, 2007, 12:58:15 AM9/7/07
to pyth...@googlegroups.com
搞不懂,没出现过这种问题.你换成英文名字先试一下,看是不是因为文件名有中文造成的再说.

chunlin zhang

unread,
Sep 7, 2007, 1:04:14 AM9/7/07
to pyth...@googlegroups.com
见鬼了,刚才输的怎么都显示不全,不知道是 google groups 的问题还是 谷歌输入法的问题...

so,I have to use english.
my db file name is english,there is no chinese char...

but when i copy app dir to "desktop",there is chinese char("桌面",like
C:\Documents and Settings\zhangcl\桌面\t ) in the full db path,because i
get the abs path of db file using os.join(appdir,"my.db").

Jiahua Huang

unread,
Sep 7, 2007, 2:17:18 AM9/7/07
to pyth...@googlegroups.com
Windows 下系统编码的问题

你可以简单加一段::
import sys
reload(sys)
sys.setdefaultencoding('gb18030')

如果你的 windows 版本不支持 gb18030 , 就用 mbcs 代替。

在 07-9-7,chunlin zhang<zhangc...@gmail.com> 写道:

chunlin zhang

unread,
Sep 7, 2007, 3:30:14 AM9/7/07
to pyth...@googlegroups.com
加上这个还是一样...

我觉得是pysqlite的问题.

C:\Documents and Settings\zhangclb\桌面\mtk.ref_list\ref_list.db
Traceback (most recent call last):
File "D:\Python25\Lib\site-packages\wx-2.8-msw-unicode\wx\_core.py", line 1409
9, in <lambda>
lambda event: event.callable(*event.args, **event.kw) )
File "C:\Documents and Settings\zhangclb\桌面\mtk.ref_list\ref_list_sync.py",
line 44, in on_initialize


self.string_standard_db_connect = sqlite.connect(filepath.encode('cp936'))
pysqlite2.dbapi2.OperationalError: unable to open database file

limodou

unread,
Sep 7, 2007, 3:31:41 AM9/7/07
to pyth...@googlegroups.com
On 9/7/07, chunlin zhang <zhangc...@gmail.com> wrote:
> 加上这个还是一样...
>
> 我觉得是pysqlite的问题.
>
> C:\Documents and Settings\zhangclb\桌面\mtk.ref_list\ref_list.db
> Traceback (most recent call last):
> File "D:\Python25\Lib\site-packages\wx-2.8-msw-unicode\wx\_core.py", line 1409
> 9, in <lambda>
> lambda event: event.callable(*event.args, **event.kw) )
> File "C:\Documents and Settings\zhangclb\桌面\mtk.ref_list\ref_list_sync.py",
> line 44, in on_initialize
> self.string_standard_db_connect = sqlite.connect(filepath.encode('cp936'))
> pysqlite2.dbapi2.OperationalError: unable to open database file
>
目录不要使用中文看一看.

chunlin zhang

unread,
Sep 7, 2007, 3:42:09 AM9/7/07
to pyth...@googlegroups.com
目录不用中文没问题的.
我这个工具已经完成了,但是发现拷到桌面上就用不了.

算了,这个不解决也没关系.发布的时候说明一下.

或者写段代码检查一下路径是否有中文,如果有中文就提示错误吧.

On 9/7/07, limodou <lim...@gmail.com> wrote:

Jiahua Huang

unread,
Sep 7, 2007, 3:46:10 AM9/7/07
to pyth...@googlegroups.com
这 utf8 环境下中文路径、文件名都没有问题的

>>> import sqlite3
>>> con = sqlite3.connect('/tmp/中文/目录/文件.db')
>>>

是不是你的版本有问题?

在 07-9-7,chunlin zhang<zhangc...@gmail.com> 写道:

> 目录不用中文没问题的.
> 我这个工具已经完成了,但是发现拷到桌面上就用不了.

chunlin zhang

unread,
Sep 7, 2007, 3:57:29 AM9/7/07
to pyth...@googlegroups.com
呀,提醒我了,我改成
sqlite.connect(filepath.encode('utf8'))
就好了.所以sqlite里都要用utf8的
里面的sql语句和数据等等都用 utf8 的,结果把这里给忘了.

谢谢jiahua!

On 9/7/07, Jiahua Huang <jhuang...@gmail.com> wrote:

Jiahua Huang

unread,
Sep 7, 2007, 4:25:19 AM9/7/07
to pyth...@googlegroups.com
Zoomq 大妈把这 utf8 的事情加啄木鸟 sqlite 栏吧,
总有需要中文目录下能用的时候

在 07-9-7,chunlin zhang<zhangc...@gmail.com> 写道:
> 呀,提醒我了,我改成
> sqlite.connect(filepath.encode('utf8'))
> 就好了.所以sqlite里都要用utf8的
> 里面的sql语句和数据等等都用 utf8 的,结果把这里给忘了.
>
> 谢谢jiahua!
>
> On 9/7/07, Jiahua Huang <jhuang...@gmail.com> wrote:
> > 这 utf8 环境下中文路径、文件名都没有问题的
> >
> > >>> import sqlite3
> > >>> con = sqlite3.connect('/tmp/中文/目录/文件.db')
> > >>>
> >
> > 是不是你的版本有问题?
> >
> > 在 07-9-7,chunlin zhang<zhangc...@gmail.com> 写道:
> > > 目录不用中文没问题的.
> > > 我这个工具已经完成了,但是发现拷到桌面上就用不了.
>
> >
>


--
Hiweed-Debian
pgpkey: http://pgp.mit.edu:11371/pks/lookup?op=get&search=0x5B6F0C29

Reply all
Reply to author
Forward
0 new messages