通过 psycopg2 对 postgresql 查询的酸洗结果
Pickeling results of postgresql query via psycopg2
我正在通过 psycopg2 库查询 PostgreSQL 数据库。这种查询的响应是 cursor object file. Because of the size of the query, I'm trying to avoid re-query it and instead - save the query result as a pickle.
不幸的是,当我尝试为此执行代码时:
import psycopg2
import pickle
# Connect to an existing database
conn = psycopg2.connect(dbname="DB", user="my_user", password="****", host="12.34.56.78")
# Open a cursor to perform database operations
cur = conn.cursor()
# Query the database and obtain data as Python objects
cur.execute("SELECT * FROM my_table[...];")
# Attempt to pickle the output
pickle_out = open("output.pickle","wb")
pickle.dump(cur, pickle_out)
pickle_out.close()
# Close communication with the database
cur.close()
conn.close()
出现错误消息:
TypeError: can't pickle psycopg2.extensions.cursor objects
通过 python 保存 SQL 查询结果以供将来使用的直接方法是什么?
注意:我没有义务使用泡菜。这对我来说似乎是一个最佳解决方案。
我认为您在 cur.execute()
及其变体之后需要 cur.fetchall()
。
https://psycopg.org/docs/cursor.html了解更多详情。
例如:
import numpy as np
import psycopg2
import pickle
# Connect to an existing database
conn = psycopg2.connect(dbname="DB", user="my_user", password="****", host="12.34.56.78")
# Open a cursor to perform database operations
cur = conn.cursor()
# Query the database and obtain data as Python objects
cur.execute("SELECT * FROM my_table[...];")
cur_out = np.asarray(cur.fetchall())
# Attempt to pickle the output
# Attempt to pickle the output
pickle_out = open("output.pickle","wb")
pickle.dump(cur_out, pickle_out)
pickle_out.close()
我正在通过 psycopg2 库查询 PostgreSQL 数据库。这种查询的响应是 cursor object file. Because of the size of the query, I'm trying to avoid re-query it and instead - save the query result as a pickle.
不幸的是,当我尝试为此执行代码时:
import psycopg2
import pickle
# Connect to an existing database
conn = psycopg2.connect(dbname="DB", user="my_user", password="****", host="12.34.56.78")
# Open a cursor to perform database operations
cur = conn.cursor()
# Query the database and obtain data as Python objects
cur.execute("SELECT * FROM my_table[...];")
# Attempt to pickle the output
pickle_out = open("output.pickle","wb")
pickle.dump(cur, pickle_out)
pickle_out.close()
# Close communication with the database
cur.close()
conn.close()
出现错误消息:
TypeError: can't pickle psycopg2.extensions.cursor objects
通过 python 保存 SQL 查询结果以供将来使用的直接方法是什么?
注意:我没有义务使用泡菜。这对我来说似乎是一个最佳解决方案。
我认为您在 cur.execute()
及其变体之后需要 cur.fetchall()
。
https://psycopg.org/docs/cursor.html了解更多详情。
例如:
import numpy as np
import psycopg2
import pickle
# Connect to an existing database
conn = psycopg2.connect(dbname="DB", user="my_user", password="****", host="12.34.56.78")
# Open a cursor to perform database operations
cur = conn.cursor()
# Query the database and obtain data as Python objects
cur.execute("SELECT * FROM my_table[...];")
cur_out = np.asarray(cur.fetchall())
# Attempt to pickle the output
# Attempt to pickle the output
pickle_out = open("output.pickle","wb")
pickle.dump(cur_out, pickle_out)
pickle_out.close()