编程错误:(psycopg2.errors.UndefinedColumn),在使用sqlalchemy时

2024-06-01 06:29:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我很难查询在postgres db(本地)上使用sqlalchemy创建的表

当我能够执行并接收查询结果时:

SELECT * FROM olympic_games 

尝试访问单列或对表执行任何其他操作时,收到错误消息:

SELECT games FROM olympic_games

错误消息是(从波兰语翻译过来的两句话):

ProgrammingError: (psycopg2.errors.UndefinedColumn) BŁĄD: column "games" does not exist

LINE 1: SELECT COUNT(Sport)
^
HINT: maybe you meant "olympic_games.Games".

SQL: SELECT games FROM olympic_games LIMIT 5;]
(Background on this error at: http://sqlalche.me/e/f405)

这相当于程序看不到,或者可以访问特定列,并显示它不存在

我尝试使用table.column格式访问,但效果不太好。我还可以通过information_schema.columns查看列名

数据(.csv)加载了pd.read_csv,然后加载了DataFrame.to_sql。代码如下,谢谢你的帮助

engine = create_engine('postgresql://:@:/olympic_games')

with open('olympic_athletes_2016_14.csv', 'r') as file:
    df = pd.read_csv(file, index_col='ID')
df.to_sql(name = 'olympic_games', con = engine, if_exists = 'replace', index_label = 'ID')

返回的两个execute命令都有相同的错误:

with engine.connect() as con:
    rs = con.execute("SELECT games FROM olympic_games LIMIT 5;")
    df_fetch = pd.DataFrame(rs.fetchall())
df_fetch2 = engine.execute("""SELECT games FROM olympic_games LIMIT 5;""").fetchall()

Tags: csvfrom消息dfreadexecute错误column
2条回答

本质上,这是PostgreSQLmanual中提到的列标识符的双引号问题:

Quoting an identifier also makes it case-sensitive, whereas unquoted names are always folded to lower case. For example, the identifiers FOO, foo, and "foo" are considered the same by PostgreSQL, but "Foo" and "FOO" are different from these three and each other.

当任何Pandas数据帧列具有混合大小写时,DataFrame.to_sql通过在CREATE TABLE阶段创建带有双引号的列来保留大小写敏感性。具体来说,下面的Python代码在使用replace

df.to_sql(name='olympic_games', con=engine, if_exists='replace', index_label='ID')

如果Sport是数据框中有标题的case列,则在Postgres中翻译如下:

DROP TABLE IF EXISTS public."olympic_games";

CREATE TABLE public."olympic_games"
(
    ...
    "Sport" varchar(255)
    "Games" varchar(255)
    ...
);

一旦一个标识符被混合引用,就必须始终以这种方式引用它。因此sport"Sport"不同。请记住,在SQL中,双引号实际上不同于在Python中可以互换的单引号

< P>修复,考虑将所有的大熊猫列呈现为小写,因为^ {CD6>}与^ {< CD7>}、^ {< CD8>}或^ {CD9>}(但不是^ {< CD10>}或^ {CD11>})相同。p>
df.columns = df.columns.str.lower()
df.to_sql(name='olympic_games', con=engine, if_exists='replace', index_label='ID')

或者,保持原样并适当报价:

SELECT "Games" FROM olympic_games

试试SELECT "games" FROM olympic_games。在某些情况下,PostgreSQL会在列名称周围创建引号。例如,如果列名包含混合寄存器。我必须提醒你:PostgreSQL是区分大小写的

相关问题 更多 >