Python论坛  - 讨论区

标题:[python-chinese] 可否将dict类型直接存入数据库

2007年04月28日 星期六 10:44

ro rosettas在gmail.com
星期六 四月 28 10:44:31 HKT 2007

hi all

看过limodou的邮件说karrigell的dbstorage可以将dict类型直接存入数据库
不知道sqlobject是否有这种功能呢

-- 
with kind regards

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

2007年04月28日 星期六 10:59

yhwu yhwu在b-star.cn
星期六 四月 28 10:59:49 HKT 2007

¿ÉÄÜÎÒÕâ±ßµÄ±àÂë³öÁËÎÊÌ⣬ÎÒ·¢ÁËһЩÎÞЧµÄÓʼþ¡£±§Ç¸£¡


yhwu
2007-04-28
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070428/00577347/attachment.htm 

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

2007年04月28日 星期六 11:09

limodou limodou在gmail.com
星期六 四月 28 11:09:53 HKT 2007

On 4/28/07, yhwu <yhwu在b-star.cn> wrote:
>
>
> 可能我这边的编码出了问题,我发了一些无效的邮件。抱歉!
>
>
在svn中,3.7


-- 
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]

2007年04月28日 星期六 11:11

danielsong sjtusongyi在gmail.com
星期六 四月 28 11:11:51 HKT 2007

¿ÉÒÔͨ¹ýpickleÄ£¿é°Ñdict¶ÔÏóдÈëÎļþ
²»ÖªµÀÎļþ¸údbÐÔÄÜÏà²î¶àÉÙ£¿



2007/4/28, ro <rosettas在gmail.com>:
>
> hi all
>
> ¿´¹ýlimodouµÄÓʼþ˵karrigellµÄdbstorage¿ÉÒÔ½«dictÀàÐÍÖ±½Ó´æÈëÊý¾Ý¿â
> ²»ÖªµÀsqlobjectÊÇ·ñÓÐÕâÖÖ¹¦ÄÜÄØ
>
> --
> with kind regards
> _______________________________________________
> 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




-- 
Best Regrads,
DanielSong
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070428/fd1ab8ff/attachment.html 

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

2007年04月28日 星期六 11:23

ro rosettas在gmail.com
星期六 四月 28 11:23:08 HKT 2007

On 4/28/07, danielsong <sjtusongyi在gmail.com> wrote:
> 可以通过pickle模块把dict对象写入文件
> 不知道文件跟db性能相差多少?

算法得当的话绝对比db的性能高
不过麻烦,呵呵

我想到的办法是转换成字符串存进去然后取出来再转换成dict
name1={'title':'HelloWorldTitle','content':'HelloWorld'}
str1 = str(nameSpace)
name2 = eval(str1)

-- 
with kind regards

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

2007年04月28日 星期六 12:07

jessinio smith jessinio在gmail.com
星期六 四月 28 12:07:39 HKT 2007

´N¬O¥Îpickle¥i¥H¡C
-------------- 下一部分 --------------
¤@?HTMLªþ¥ó³Q²¾°£...
URL: http://python.cn/pipermail/python-chinese/attachments/20070428/3c8fc0fc/attachment.htm 

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

2007年04月28日 星期六 12:25

danielsong sjtusongyi在gmail.com
星期六 四月 28 12:25:10 HKT 2007

pickle.dump/load

ÔÚ07-4-28£¬ro <rosettas在gmail.com> дµÀ£º
>
> On 4/28/07, danielsong <sjtusongyi在gmail.com> wrote:
> > ¿ÉÒÔͨ¹ýpickleÄ£¿é°Ñdict¶ÔÏóдÈëÎļþ
> > ²»ÖªµÀÎļþ¸údbÐÔÄÜÏà²î¶àÉÙ£¿
>
> Ëã·¨µÃµ±µÄ»°¾ø¶Ô±ÈdbµÄÐÔÄܸß
> ²»¹ýÂé·³,ºÇºÇ
>
> ÎÒÏëµ½µÄ°ì·¨ÊÇת»»³É×Ö·û´®´æ½øÈ¥È»ºóÈ¡³öÀ´ÔÙת»»³Édict
> name1={'title':'HelloWorldTitle','content':'HelloWorld'}
> str1 = str(nameSpace)
> name2 = eval(str1)
>
> --
> with kind regards
> _______________________________________________
> 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




-- 
Best Regrads,
DanielSong
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070428/b2877174/attachment.html 

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

2007年04月29日 星期日 08:36

ro rosettas在gmail.com
星期日 四月 29 08:36:53 HKT 2007

On 4/28/07, danielsong <sjtusongyi在gmail.com> wrote:
>
> pickle.dump/load

这两个函数针对sqlobject怎么玩

-- 
with kind regards

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

2007年04月29日 星期日 08:56

Hong Yuan hongyuan在homemaster.cn
星期日 四月 29 08:56:33 HKT 2007

ro 写道:
> On 4/28/07, danielsong <sjtusongyi在gmail.com> wrote:
>   
>> pickle.dump/load
>>     
>
> 这两个函数针对sqlobject怎么玩
>
>   
把pickle后的结果通过sqlobject使用Binary数据类型保存。不过这样的话就无法 
用SQL来操纵数据对象了,除非使用SQL服务器的 Python扩展。

-- 

大管家网上建材超市
装修买建材 上网找大管家
http://www.homemaster.cn
Tel: 0086-21-34240987
Fax: 0086-21-64692422


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

2007年04月29日 星期日 09:34

ro rosettas在gmail.com
星期日 四月 29 09:34:25 HKT 2007

> 把pickle后的结果通过sqlobject使用Binary数据类型保存。不过这样的话就无法
> 用SQL来操纵数据对象了,除非使用SQL服务器的 Python扩展。

OK,明白
但是似乎应该是string数据类型

另外我检讨一下,没有看文档,实际sqlobject有提供一个PickleCol类型.可以直接保存python对象

-- 
with kind regards

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

2007年04月29日 星期日 09:47

Hong Yuan hongyuan在homemaster.cn
星期日 四月 29 09:47:21 HKT 2007

ro 写道:
>> 把pickle后的结果通过sqlobject使用Binary数据类型保存。不过这样的话就无法
>> 用SQL来操纵数据对象了,除非使用SQL服务器的 Python扩展。
>>     
>
> OK,明白
> 但是似乎应该是string数据类型
>
> 另外我检讨一下,没有看文档,实际sqlobject有提供一个PickleCol类型.可以直接保存python对象
>
>   
Pickle的结果是二进制的数据,使用string类型会有潜在风险。如果数据库支持 
binary类型,尽量使用binary类型。

-- 

大管家网上建材超市
装修买建材 上网找大管家
http://www.homemaster.cn
Tel: 0086-21-34240987
Fax: 0086-21-64692422


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

2007年04月29日 星期日 11:07

jessinio smith jessinio在gmail.com
星期日 四月 29 11:07:42 HKT 2007

µ½µ×ÔÚÕâ¸öÎÊÌâÉÏ£¬textÀàÐÍÓëbinaryÀàÐÍÓÐÊ²Ã´Çø±ð£¿£¿
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070429/fc6b730f/attachment.htm 

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

2007年04月29日 星期日 11:54

Hong Yuan hongyuan在homemaster.cn
星期日 四月 29 11:54:24 HKT 2007

jessinio smith 写道:
> 到底在这个问题上,text类型与binary类型有什么区别??
By default, the pickle data format uses a printable ASCII
representation. This is slightly more voluminous than a binary
representation. The big advantage of using printable ASCII (and of some
other characteristics of pickle's representation) is that for debugging
or recovery purposes it is possible for a human to read the pickled file
with a standard text editor.

There are currently 3 different protocols which can be used for pickling.

    * Protocol version 0 is the original ASCII protocol and is backwards
      compatible with earlier versions of Python.
    * Protocol version 1 is the old binary format which is also
      compatible with earlier versions of Python.
    * Protocol version 2 was introduced in Python 2.3. It provides much
      more efficient pickling of new-style classes.

如果选择旧的ascii的pickle协议,那没有什么区别。但version1和version2的结
果是二进制的。数据库中text类型和 binary类型的区别如下(摘自postgresql help):

Binary strings are distinguished from character strings by two
characteristics: First, binary strings specifically allow storing octets
of value zero and other “non-printable” octets (usually, octets outside
the range 32 to 126). Character strings disallow zero octets, and also
disallow any other octet values and sequences of octet values that are
invalid according to the database's selected character set encoding.
Second, operations on binary strings process the actual bytes, whereas
the processing of character strings depends on locale settings. In
short, binary strings are appropriate for storing data that the
programmer thinks of as “raw bytes”, whereas character strings are
appropriate for storing text.

一句话,如果不考虑效率,那选择用旧的pickle协议,保存为可打印的ascii字
符。如果效率要求高的话,那建议使用version 2协议,数据库中保存为binary类型。

另外建议使用cPickle模块,速度快些。

-- 

大管家网上建材超市
装修买建材 上网找大管家
http://www.homemaster.cn
Tel: 0086-21-34240987
Fax: 0086-21-64692422


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

2007年04月29日 星期日 12:47

jessinio smith jessinio在gmail.com
星期日 四月 29 12:47:53 HKT 2007

µÚ¶þ¶ÎÓ¢ÎÄÓеãÄѶȣ¡£¨EÎIJ»ºÃ£©
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070429/6a4b384e/attachment.html 

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

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

    你的回复:

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

    Zeuux © 2025

    京ICP备05028076号