繁体   English   中英

psycopg2.errors.UndefinedColumn:列

[英]psycopg2.errors.UndefinedColumn: column

尝试获取所有数据时收到此错误消息

Traceback (most recent call last):
  File "/home/user/xxxxx/node_modules/serverless/lib/plugins/aws/invokeLocal/runtimeWrappers/invoke.py", line 86, in <module>
    result = handler(input['event'], context)
  File "./src/handler.py", line 97, in historical
    post_process(db_connection)
  File "./src/post_process.py", line 25, in post_process
    averages = db_connection.fetch_all("""
  File "./src/common/database_helper.py", line 36, in fetch_all
    cursor.execute(query)
psycopg2.errors.UndefinedColumn: column "col_list" does not exist
LINE 4:                                            avg(col_list[z])

代码

col_list = ['A', 'B', 'C']

averages = db_connection.fetch_all("""
                                           SELECT
                                           DATE_TRUNC('month', "DateTime") AS date_time,
                                           avg(col_list[z])
                                           FROM public."price_NA" group by DATE_TRUNC('month', "DateTime")
                                           ORDER BY date_time DESC
                                           """
                                           )

你真的认为这会奏效吗? SQL 对 Python 变量一无所知。 幸运的是,使用 f-strings 很容易解决:

col_list = ['A', 'B', 'C']

averages = db_connection.fetch_all(f"""
                                           SELECT
                                           DATE_TRUNC('month', "DateTime") AS date_time,
                                           avg({col_list[z]})
                                           FROM public."price_NA" group by DATE_TRUNC('month', "DateTime")
                                           ORDER BY date_time DESC
                                           """
                                           )

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM