【问题标题】:Storing a pickled dictionary in Postgresql (Psycopg2)在 Postgresql (Psycopg2) 中存储腌制字典
【发布时间】:2020-07-18 16:41:13
【问题描述】:

我正在尝试在 Postgresql 中存储一个腌制的嵌套字典(我知道这是一种快速而肮脏的方法,并且无法从 Postgresql 访问字典内容 - 通常是不好的做法)

# boilerplate, preamble and upstream work.
import psycopg2


''' Inputs: nd = dictionary to be pickled '''



pickled = pickle.dumps(nd)

connection = psycopg2.connect(user = "-----",
                              password = "----",
                              host = "----",
                              port = "----",
                              database = "----")
    
name = 'database1'    
    
    
print('Connected...')
cursor = connection.cursor()
print(connection.get_dsn_parameters(),"\n")
cursor.execute("CREATE TABLE thetable (name TEXT, ablob BYTEA)")
print('Created Table...')
cursor.execute("INSERT INTO thetable VALUES(%s)",(psycopg2.Binary(pickled),))
connection.commit()
print('Added Data...')
cursor.close()
connection.close()
print('Connection closed...')

当我进行数据数据检索时,我在从 Postgres 导入数据时遇到了很多问题 - 本质上是要打开数据,将其解压缩回字典并进行可视化。我试过了:

import psycopg2
from io import BytesIO

connection = psycopg2.connect(user = "----",
                              password = "----",
                              host = "----",
                              port = "----",
                              database = "----")

cursor = connection.cursor()
cursor.execute("SELECT ablob FROM thetable")
result, = cursor.fetchone()
cursor.close()
connection.rollback()

result = BytesIO(result)

print(pickle.load(result))

根据此链接:https://www.oreilly.com/library/view/python-cookbook/0596001673/ch08s08.html,并咨询:Insert an image in postgresql database 和:saving python object in postgres table with pickle,但是已经无法返回腌制字典。

非常感谢任何实现这一目标的建议!

【问题讨论】:

  • 你确定你的代码运行没有抛出异常吗?您的插入应该类似于:cursor.execute("INSERT INTO thetable VALUES(%s, %s)",('some name', psycopg2.Binary(pickled),)) 另外,我从未听说过 psycopg2.Binary() 包装器。我只是传递了b'' 对象。
  • 它不会抛出任何异常,当我检查 Heroku Postgres 数据库时,'abloom' 和 'name' 都存在于表中(虽然不确定是否填充)
  • @Mike Organek。根据文档Binary
  • 我会检查表中是否有数据。我和@Mike Organek 在一起,我不知道"INSERT INTO thetable VALUES(%s)",(psycopg2.Binary(pickled),) 怎么能成功。您需要更改为 "INSERT INTO thetable(ablob) VALUES(%s)",(psycopg2.Binary(pickled),) 或 Mike 建议的内容。
  • 更新我之前的评论。因为您没有指定列并且只提供了一个值,所以该值将被插入到name 中,因为INSERT 按声明的顺序与提供的VALUES 1:1 匹配列。只要要插入的字符串不违反字符规则,它就会成功。在这种情况下,ablob 将设置为 NULL

标签: python postgresql dictionary pickle


【解决方案1】:

当您的CREATE TABLE 列出两个字段时,您必须在 INSERT 中列出要填写的字段,除非您全部填写。

import psycopg2
import pickle

dict = {
  "foo": "bar"
}

p = pickle.dumps(dict)

connection = psycopg2.connect(database = "test")

cursor = connection.cursor()
cursor.execute("CREATE TABLE thetable (name TEXT, ablob BYTEA)")
cursor.execute("INSERT INTO thetable VALUES(%s,%s)",('test',p))
connection.commit()
cursor.close()
connection.close()

阅读

import psycopg2
import pickle

connection = psycopg2.connect(database = "test")

cursor = connection.cursor()
cursor.execute("SELECT ablob FROM thetable WHERE name='test';")
result = cursor.fetchone()

print pickle.loads(result[0])

cursor.close()
connection.close()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-15
    • 2016-01-05
    • 1970-01-01
    • 1970-01-01
    • 2013-05-02
    • 1970-01-01
    • 2017-07-29
    相关资源
    最近更新 更多