本文介绍了在关系数据库中存储对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几周前我开始玩python几年之后

的perl编程我可以说我的第一印象是,

不出所料,非常积极。 ;)

我在这里写的原因是我似乎无法弄清楚如何将
保存/恢复python对象到关系数据库中。我在perl中用它做b
的方式就是在将对象存储到数据库之前冻结对象,并在恢复它之前解冻它。 。 (对于那些不熟悉perl术语的人来说,冻结和解冻是来自

perl'的持久性模块Storable的方法)。我发现python的'

对应的模块是Pickle但它似乎对我不起作用..我

大致如下:

类TestA:

def insert():

i = TestA(''asdf'')

output = cStringIO。 StringIO()

cPickle.dump(i,output,2)

print" output.getvalue():%s" %output.getvalue()

jd = libpq.PgQuoteBytea(output.getvalue())

print" jd%s" %jd

返回dbpool.runOperation("插入作业(effective_job_id,

job_description)值(1," + jd +")")

解决方案



请重新阅读pickle.dump,pickle.dumps,pickle.load和

pickle.loads的文档。你在这里不需要StringIO,只需使用函数的'b
版本。



我不知道你是否将它标记为优雅,但就我而言,

将序列化对象存储为关系数据库中的blob大多数是无意义的b $ b。如果我使用关系数据库,那是因为它是一个

*关系*数据库。如果你想要一个OODB,那么我们有ZODB,

Durus和其他几个。




包含追溯,或者至少包含

的最后3-4行通常是有帮助的,如它可以帮助每个人解决你需要的问题。


如果你不确定函数中的哪一行导致了这个问题,尝试

评论除了第一个,运行测试,重新添加下一个,运行 -

和 - 测试等


但错误很可能是这一行:



cPickle.load文件中的unpickles,但是你在这里给它一个

字符串。你想要cPickle.loads。


在翻译中,你总是可以通过''帮助(cPickle.loads)查看文档字符串来快速查看这些文件。 )''(或''cPickle.loads?''如果

你正在使用iPythhon)。


- alex23

I started playing with python a few weeks ago after a number of years
of perl programming and I can say that my first impression is,
unsurprisingly, quite positive. ;)
The reason I am writing here is that I can''t seem to figure out how to
save/restore python objects into a relational database. The way I
used to do it in perl was to ''freeze'' the object before storing it
into the database and ''thaw'' it before restoring it. (For those not
familiar with the perl terminology freeze and thaw are method from
perl''s persistence module Storable). I found that the python''s
corresponding module is Pickle but it doesn''t seem to work for me.. I
do roughly the following:
class TestA:
def insert():
i = TestA(''asdf'')
output = cStringIO.StringIO()
cPickle.dump(i, output, 2)
print "output.getvalue(): %s" % output.getvalue()
jd = libpq.PgQuoteBytea(output.getvalue())
print "jd %s" % jd
return dbpool.runOperation("insert into jobs (effective_job_id,
job_description) values (1, " + jd + ")")

解决方案


Please reread the doc for pickle.dump, pickle.dumps, pickle.load and
pickle.loads. You just don''t need StringIO here, just use the ''s
versions of the functions.

I don''t know if you''d label it ''elegant'', but as far as I''m concerned,
storing serialized objects as blobs in a relational database is mostly
non-sense. If I use a relational database, it''s because it is a
*relational* database. If you want an OODB, then we have the ZODB,
Durus and a couple others.


It''s often helpful to include the traceback, or at the very least the
last 3-4 lines of it, as it helps everyone work out the issue you''re
having.

If you''re not sure which line in a function is causing the issue, try
commenting out all but the first, run-and-test, re-add the next, run-
and-test etc

But the error is most likely this line:

cPickle.load unpickles from a file, but here you''re handing it a
string. You want cPickle.loads.

At the interpreter, you can always quickly check these out by looking
up the docstring via ''help(cPickle.loads)'' (or ''cPickle.loads?'' if
you''re using iPythhon).

- alex23


这篇关于在关系数据库中存储对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 22:35
查看更多