如何将 python 对象插入 postgreSQL

How can I INSERT a python object into postgreSQL

我在 Whosebug 中发现了这个问题 saving python object in postgres table with pickle

I have a python script which creates some objects. I would like to be able to save these objects into my postgres database for use later. Following the comment from @SergioPulgarin I tried the following which worked!

Storing:

  1. Pickle the object to a binary string pickle_string = pickle.dumps(object)
  2. Store the pickle string in a bytea (binary) field in postgres. Use simple INSERT query in Psycopg2

Retrieval:

  1. Select the field in Psycopg2. (simple SELECT query)
  2. Unpickle the decoded result retrieved_pickle_string = pickle.loads(decoded_result)

我转储了 pickle 文件。现在我试图将它加载到数据库中。我的问题是我找不到正确的语法来做到这一点...

我已经在带有 psycopg2 的 python 文件中进行了尝试:我已经转储了 pickle 对象并连接到数据库。我试图将数据类型更改为字节,然后将 pickle 插入数据库:

pickle_obj = bytes(pickle_file)
query = "INSERT INTO schema.table (col1, ...) VALUES (%%s, ...)" % pickle_obj
cursor.execute(query)

我收到这个错误:

Traceback (most recent call last):
  File "/path/to/file.py", line 18, in <module>
    cursor.execute(query)
psycopg2.errors.SyntaxError: syntax error at or near "K"
LINE 1: ...e1_h\x15Nh\x16K\x00h\x17Nh\x18Nh\x19\x89h&K\x0bh\'K\x01h(h+h...
                                                         ^

进程已完成,退出代码为 1

然后我尝试直接从 postgreSQL 插入它(但我也不确定这段代码,因为它也不起作用。)

INSERT INTO table SELECT PG_Read_File('/home/...)

我认为我的语法不是 100% 正确?那你是怎么做到的?感谢您的提示!

Try to wrap this object with psycopg2.Binary as stated in the docs. https://www.psycopg.org/docs/usage.html#adapt-binary

这些帖子似乎被删除了,但因为它对我有用,所以我只是在这里添加它。谢谢!

您应该始终使用参数化查询将值代入查询。字符串构造或格式化会使您面临 SQL 注入攻击。除了安全优势,使用参数化查询允许驱动程序为您处理转义和引用。

完成您所要求的最短路径是在 table 中使用 bytea 列。

如果您的 table 看起来像这样:

  Column   |  Type   
-----------+---------
 id        | integer serial primary key
 bytesdata | bytea   

那么这个 insert 就可以了:

cursor.execute("insert into mytable (bytesdata) values (%s) returning id", (pickle_obj, ))
inserted_id = cursor.fetchone()[0]
connection.commit()

稍后要检索此对象,您可以执行:

cursor.execute("select bytesdata from mytable where id = %s", (inserted_id, ))
row = cursor.fetchone()
retrieved_pickle_obj = row[0]