psycopg2.errors.SELECT 查询上出现 UndefinedTable 错误

问题描述 投票:0回答:1

简介

我正在编写的脚本从格式为

.csv
schema, table, column
文件中读取,然后执行
SELECT
查询以获取这些列的所有记录的值。我的脚本目标是打印出
.csv
文件中找到的所有列的值。

问题

运行脚本时,我收到

psycopg2.errors.UndefinedTable
方法的
cursor.execute()
错误:

print(f'SELECT "{column}" FROM {schema}."{table}";') # print out query for debugging
cursor.execute(f'SELECT "{column}" FROM {schema}."{table}";')

# Output (Trimmed)
SELECT "CREATION_DATE" FROM abbotsley_271."AREA_BUILD_PHASE_BOUNDARIES";
psycopg2.errors.UndefinedTable: relation "abbotsley_271.AREA_BUILD_PHASE_BOUNDARIES" does not exist
LINE 1: SELECT "CREATION_DATE" FROM abbotsley_271."AREA_BUILD_PHASE...

正如您从输出中看到的,它抱怨关系

"abbotsley_271.AREA_BUILD_PHASE_BOUNDARIES"
不存在,尽管 PostgreSQL 数据库中确实存在该架构和表。

如果我在 DBeaver 等 DBMS 工具上运行

print()
的打印查询,则查询工作正常:

此外,如果我用打印的查询替换

execute()
方法中的 fstrings,它也可以正常工作:

cursor.execute('SELECT "CREATION_DATE" FROM abbotsley_271."AREA_BUILD_PHASE_BOUNDARIES";')

# Output
PS C:\Users\user> & "C:/Program Files/Python311/python.exe" "g:/My Drive/Code Library/Python/Data - character varying to timestamp without time zone.py"
SELECT "CREATION_DATE" FROM abbotsley_271."AREA_BUILD_PHASE_BOUNDARIES";
[('2022-07-08 21:53:23',), ('2023-03-13 05:47:28',), ('2022-06-28T16:27:39.000',), ('2023-03-21 20:37:25',), ('2023-02-08 10:00:58',), ('2023-03-22 10:14:39',), ('2023-06-27 16:38:05',), ('2022-06-28T16:30:33.000',), ('2023-03-21 18:53:04',), ('2022-06-28T16:29:20.000',), ('2023-06-27 16:32:05',), ('2023-01-23 06:27:06',), ('2022-07-04T12:17:18.575',), ('2022-06-28T16:29:01.000',), ('2022-06-28T16:55:00.000',), ('2022-07-04 15:31:46',), ('2022-07-06 12:11:00',), ('2022-07-06 17:00:35',), ('2022-07-07 12:13:04',), ('2022-07-12 00:00:00',), ('2022-07-08 10:55:40',), ('2022-06-28T16:30:42.000',), ('2022-07-12 00:00:00',), ('2022-06-28T16:27:53.000',), ('2022-06-28T16:28:11.000',), ('2022-06-28T16:28:22.000',), ('2022-06-28T16:28:47.000',), ('2022-06-28T16:29:35.000',), ('2022-07-08 21:53:23',), ('2022-07-12 00:00:00',), ('2022-07-12 00:00:00',), ('2022-06-28T16:30:10.000',), ('2022-06-28T16:30:19.000',), ('2023-02-17 06:59:41',), ('2022-07-12 00:00:00',), ('2022-07-12 00:00:00',), ('2022-06-28T16:33:08.000',), ('2022-07-12 00:00:00',), ('2022-06-28T16:29:55.000',), ('2023-03-21 18:33:08',), ('2022-07-12 00:00:00',), ('2023-03-21 18:47:39',), ('2022-07-08 21:53:23',), ('2023-03-21 18:53:04',)]

我将非常感谢对此的任何见解,因为我无法理解问题是什么。

完整脚本

import psycopg2
import csv
from tqdm import tqdm

conn = psycopg2.connect(
    host="IP",
    database="db",
    user="user",
    password="password")

cursor = conn.cursor()

with open("C:\\Users\\user\\Downloads\\excel_files\\character varying to timestamp.csv", 'r', encoding='utf-8') as csv_file:
    csv_rows = csv.reader(csv_file, delimiter=',')
    columnLengths = []
    
    for value in tqdm(csv_rows, desc="CSV progress"):
        schema = value[0]
        table = value[1]
        column = value[2]
        
        print(f'SELECT "{column}" FROM {schema}."{table}";')
        cursor.execute(f'SELECT "{column}" FROM {schema}."{table}";') # error location

        data = cursor.fetchall()
        print(data)

        for record in data:
            cvValue = record[0]

            columnLengths.append(f"{schema}, {table}, {column}, {cvValue}")

    for record in columnLengths:
        print(record)

我尝试过的事情

  • 我尝试过尝试各种封装类型(
    '
    vs
    "
    )但无济于事
  • 从查询中删除了
    {schema}.
    ,这避免了错误,但我认为这只是在整个循环持续时间内搜索一个模式
python postgresql psycopg2
1个回答
1
投票

使用 sql 模块的示例:

import psycopg2
from psycopg2 import sql

con = psycopg2.connect("dbname=test host=localhost  user=postgres")
cur = con.cursor()


tbl_qry = sql.SQL("SELECT {} FROM {}").format(sql.Identifier("CREATION_DATE"), 
                  sql.Identifier("abbotsley_271", "AREA_BUILD_PHASE_BOUNDARIES"))

print(tbl_qry.as_string(con))
SELECT "CREATION_DATE" FROM "abbotsley_271"."AREA_BUILD_PHASE_BOUNDARIES"

cur.execute(tbl_qry)

© www.soinside.com 2019 - 2024. All rights reserved.