【问题标题】:Can not print updatedAT column from Postgres无法从 Postgres 打印 updatedAT 列
【发布时间】:2016-08-10 21:00:07
【问题描述】:

我在 postgres 中有一个表,我只想获取 updatedAt 列。如果我打印表格列,updateAt 存在:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import psycopg2
import sys
import datetime

con = None

try:

    con = psycopg2.connect(database='db', user='dbuser', password='1111')
    cur = con.cursor()
    cur.execute('SELECT * from incidents')

    column_names = [row[0] for row in cur.description]

    print("Column names: %s\n" % column_names)


except psycopg2.DatabaseError as e:
    print('Error %s' % e)
    sys.exit(1)


finally:

    if con:
        con.close()

结果是

Column names: ['id', 'deviceId', 'latitude', 'longitude', 'city', 'area', 'address', 'state', 'postalCode', 'country', 'canceled', 'createdAt', 'updatedAt', 'patientId']

但是当我尝试从该表中选择所有行并仅获取 updatedAt 信息时,它说该列不存在。

#!/usr/bin/python
# -*- coding: utf-8 -*-

import psycopg2
import sys
import datetime

con = None

try:

    con = psycopg2.connect(database='db', user='dbuser', password='1111')
    cur = con.cursor()
    cur.execute('SELECT updatedAt from incidents')
    rows = cur.fetchall()

    for row in rows:
        print(row)


except psycopg2.DatabaseError as e:
    print('Error %s' % e)
    sys.exit(1)


finally:

    if con:
        con.close()

错误信息是:

Error column "updatedat" does not exist
LINE 1: SELECT updatedAt from incidents
               ^
HINT:  Perhaps you meant to reference the column "incidents.updatedAt".

我也尝试过使用incidents.updatedAt,但没有成功。

编辑

忘了说这个表是用 Node.js 和 Sequelize 创建的。

【问题讨论】:

    标签: python postgresql python-3.5


    【解决方案1】:

    Postgres 列名默认为小写。由于该列名称中有一个大写字母,因此必须引用它才能被引用。如果列中有空格或特殊字符,情况也是如此。这个查询应该可以工作:

    SELECT "updatedAt" FROM incidents
    

    【讨论】:

    • 呵呵!惊人的!奇迹般有效!非常感谢!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多