简介
我正在编写的脚本从schema, table, column
的.csv
文件中读取数据,然后执行SELECT
查询以获取这些列的所有记录的值。我使用该脚本的目标是打印出.csv
文件中所有列的值。
问题
运行脚本时,我收到cursor.execute()
方法的psycopg2.errors.UndefinedTable
错误:
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)
型
What I Have Tried
- 我尝试过各种封装类型(
'
与"
),但都没有成功 - 从查询中删除了
{schema}.
,这避免了错误,但我认为这只是在整个循环期间搜索一个模式
1条答案
按热度按时间h7wcgrx31#
使用sql模块的示例:
字符串