我在 App Engine 烧瓶应用程序上使用pg8000,以便能够处理 CSV 文件并将其插入 PSQL 实例(托管在 上AZURE
)。
为什么我使用pg8000
而不是psycopg2
?-> 因为应用引擎不支持 psycopg2。
到目前为止,文档pg8000
并没有说明会像 psycopg2 那样执行此操作的函数。我还没有找到在 SO 或任何其他地方(包括文档)实现这一目标的示例。
任何人都知道这是否可能?
我在 App Engine 烧瓶应用程序上使用pg8000,以便能够处理 CSV 文件并将其插入 PSQL 实例(托管在 上AZURE
)。
为什么我使用pg8000
而不是psycopg2
?-> 因为应用引擎不支持 psycopg2。
到目前为止,文档pg8000
并没有说明会像 psycopg2 那样执行此操作的函数。我还没有找到在 SO 或任何其他地方(包括文档)实现这一目标的示例。
任何人都知道这是否可能?
查看源代码,似乎没有直接导入 CSV 的方法,代码似乎也没有任何内置的INSERT
查询包装器,因此可以
您可以选择手动使用 CSV 阅读器并使用executemany
:
import csv
import pg8000
conn = pg8000.connect(user="postgres", password="C.P.Snow")
cursor = conn.cursor()
command = 'INSERT INTO book (title) VALUES (%s), (%s) RETURNING id, title'
with open('my-data.csv', 'rb') as fl:
data = list(csv.reader(fl))
conn.executemany(command, data)
需要注意的是,根据数据的大小,最好使用islice
:
with open('my-data.csv', 'rb') as fl:
reader = csv.reader(fl)
slice = itertool.islice(reader, 100)
while slice:
conn.executemany(command, slice)
slice = itertool.islice(reader, 100)
As suggested in another question here, you could use the next
method before applying the logic on the csv files and before using the csv read method.
Sorry in advance for not inserting as a complement to the previous answer, but I don't have enough points to do so.
I'm having the same issue and I solved the problem using the below. Please notice that for me, the correct way of executing many is on cursor
object, not on the conn
.
conn = pg8000.connect(user='username', password='password', host='host', port=5432, database='database name')
cursor = conn.cursor()
command = "INSERT INTO public.salesforce_accounts (field1, field2, field3, field4, field5, field6) VALUES (%s, %s, %s, %s, %s, %s)"
with open('test.csv', 'r') as file:
next(file)
data = list(csv.reader(file))
cursor.executemany(command, data)