zlib解压头检查错误
我正在压缩并存储到 postgres 数据库的字典有问题。我可以毫无错误地压缩字典并解压缩它,但是当我将其插入数据库,然后选择它并尝试解压缩数据时,我收到错误:
zlib.error: Error -3 while decompressing data: incorrect header check
这是我编写的用于模拟所有部分的测试脚本:
import zlib
import cPickle
import psycopg2
try:
db = psycopg2.connect( database='*****', user='*****', password='*****',host='******')
cursor = db.cursor()
except:
print 'no db'
atom_id = 166503
params = {'submission': 'RVBV4SXLVVDNAAKIG2LIJKZQ', 'campaign': 'p3percybot', 'dob': '2011-03-11', 'rndrpath': '/mnt/webservices/store/p3percybot/rndr/2011-03-11/RVBV4SXLVVDNAAKIG2LIJKZQ', 'outpath': '/mnt/webservices/store/p3percybot/out/2011-03-11/RVBV4SXLVVDNAAKIG2LIJKZQ', 'srcpath': '/mnt/webservices/store/p3percybot/src', 'root': '/mnt/webservices/store', 'inpath': '/mnt/webservices/store/p3percybot/in/2011-03-11/RVBV4SXLVVDNAAKIG2LIJKZQ'}
print params
params_list = []
for k in params :
param_name = k
param_value = cPickle.dumps(params[k])
param_value = zlib.compress(param_value,9)
param_value = buffer(param_value)
params_list.append((param_value,atom_id, param_name))
print params_list
sql = 'UPDATE atomparams set value = %s where atomid=%s and name=%s'
cursor.executemany(sql, (params_list))
sql = 'SELECT name, value FROM atomparams WHERE atomid=%s'
cursor.execute(sql, (atom_id,))
result = cursor.fetchall()
print '\n-----------------result-----------------'
print result
for data in result:
print data[0]
data_string = zlib.decompress(data[1])
print data_string
Open to any关于为什么这在数据库中变得混乱的建议。我应该注意存储值的字段类型是 bytea 类型
提前致谢!
I have an issue with a dictionary I'm compressing and storing to a postgres database. I can compress the dictionary and decompress it without fault but when I insert it into the database, and then select it back and try to decompress the data I get an error:
zlib.error: Error -3 while decompressing data: incorrect header check
Here's a test script I wrote to emulate all the parts:
import zlib
import cPickle
import psycopg2
try:
db = psycopg2.connect( database='*****', user='*****', password='*****',host='******')
cursor = db.cursor()
except:
print 'no db'
atom_id = 166503
params = {'submission': 'RVBV4SXLVVDNAAKIG2LIJKZQ', 'campaign': 'p3percybot', 'dob': '2011-03-11', 'rndrpath': '/mnt/webservices/store/p3percybot/rndr/2011-03-11/RVBV4SXLVVDNAAKIG2LIJKZQ', 'outpath': '/mnt/webservices/store/p3percybot/out/2011-03-11/RVBV4SXLVVDNAAKIG2LIJKZQ', 'srcpath': '/mnt/webservices/store/p3percybot/src', 'root': '/mnt/webservices/store', 'inpath': '/mnt/webservices/store/p3percybot/in/2011-03-11/RVBV4SXLVVDNAAKIG2LIJKZQ'}
print params
params_list = []
for k in params :
param_name = k
param_value = cPickle.dumps(params[k])
param_value = zlib.compress(param_value,9)
param_value = buffer(param_value)
params_list.append((param_value,atom_id, param_name))
print params_list
sql = 'UPDATE atomparams set value = %s where atomid=%s and name=%s'
cursor.executemany(sql, (params_list))
sql = 'SELECT name, value FROM atomparams WHERE atomid=%s'
cursor.execute(sql, (atom_id,))
result = cursor.fetchall()
print '\n-----------------result-----------------'
print result
for data in result:
print data[0]
data_string = zlib.decompress(data[1])
print data_string
Open to any suggestions as to why this is getting messed up in the db. I should note the field type storing the value is of type bytea
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
二进制字符串需要转义
psycopg2
http://initd.org/psycopg/docs/usage.html#adaptation-of-python-values-to-sql-types
http://initd.org/psycopg/docs/module.html#psycopg2.Binary
Postgres 二进制字符串
https://www.postgresql.org/ docs/current/static/datatype-binary.html#DATATYPE-BINARY-SQLESC
The binary string requires escaping
psycopg2
http://initd.org/psycopg/docs/usage.html#adaptation-of-python-values-to-sql-types
http://initd.org/psycopg/docs/module.html#psycopg2.Binary
Postgres Binary String
https://www.postgresql.org/docs/current/static/datatype-binary.html#DATATYPE-BINARY-SQLESC