postgresql - "("postgresql + Python 处或附近的语法错误

标签 postgresql export-to-csv psycopg2 postgresql-8.0

我正在使用 Jupyter notebook 来处理一些带有 postgresql 的数据库 我有以下实例:

import pandas as pd
import (other packages)
conn_string= % I can't show this, but this is ok
conn = psycopg2.connect(conn_string)
cursor=conn.cursor
query= """ copy (select col1,col2 from Table where col3=a_parameter
           and col4=b_parameter) to '/tmp/test.csv' with csv """
pd.read_sql(query,conn)

但是我得到了这个错误:

**ProgrammingError: syntax error at or near "("
LINE 1: COPY (select col1,col2 from Table where col3...**
             ^

为什么复制语句有错误? 我正在使用 Postresql 8.0.2

最佳答案

像这样:

import csv
            my_file_csv =  my_folder + "\Report_Trip_Day_" + my_opr + "_" + my_local_database + ".csv"


            out = csv.writer(open(my_file_csv, "w", newline=''), delimiter=',', quoting=csv.QUOTE_ALL)
            out.writerow(colnames)
            for row in my_xls_report_table:
                out.writerow(row)

关于postgresql - "("postgresql + Python 处或附近的语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50748504/

相关文章:

SQL:处理 NULL 值(联合)

java - 检查密码是否与特定用户匹配 - PostgreSQL 和 Java

csv - 导出 Prestashop 1.6 中的所有产品字段?

excel - 如何将 Microsoft ODC 文件/Power Query 从具有超过 200 万行数据的 Excel 文件转换为 CSV 文件?

powershell - 如何在Powershell中导出具有arraylist的对象

python - psycopg2.OperationalError : FATAL: unsupported frontend protocol 1234. 5679 : server supports 2. 0 到 3.0

django - 操作错误 : FATAL: database "django" does not exist

python - 我是否正确地将 aiohttp 与 psycopg2 一起使用?

sql - 如何在不重新连接的情况下刷新 postgresql 查询?

python - 使用 Python 多处理加速从 Postgres 获取大表到 CSV