我有一個小的python腳本。我想用庫 psycopg2 執行 psql 命令:
query = "\COPY ( select * from 'schema'.structure) TO 'path' DELIMITER ',' CSV HEADER;"
但是我有一個語法錯誤\COPY。我曾嘗試與改變它''和\\,但似乎沒有任何作業。我不想使用cur.copy_from.
有什么建議嗎?
uj5u.com熱心網友回復:
如果要將psycopg2forCOPY與自定義 SQL 一起使用,則必須使用copy_expert. 假設您的查詢有效,這樣:
with open(file_path, "w") as outfile:
cur.copy_expert("COPY (select * from 'schema'.structure) TO STDOUT DELIMITER ',' CSV HEADER", outfile)
您必須提供一個可寫檔案作為第二個引數并TO STDOUT在查詢中使用。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/399537.html
標籤:Python PostgreSQL的 psycopg2 psql
