Python psycopg2查询返回None

Python psycopg2查询返回None,python,postgresql,psycopg2,Python,Postgresql,Psycopg2,一般来说,我对python和编程都是新手。这段代码最终将在更大的应用程序中使用。我为任何错误提前道歉。我正在尝试从postgresql数据库中的表进行简单查询。唯一的结果是没有。尽管我知道那里有数据。提前感谢您的帮助 import psycopg2 def connect(): """Connects to the database so you can query the stats you require""" conn= psycopg2.connect("dbname

一般来说,我对python和编程都是新手。这段代码最终将在更大的应用程序中使用。我为任何错误提前道歉。我正在尝试从postgresql数据库中的表进行简单查询。唯一的结果是没有。尽管我知道那里有数据。提前感谢您的帮助

import psycopg2

def connect():
    """Connects to the database so you can query the stats you require"""

    conn= psycopg2.connect("dbname='Nascar_Stats' user='postgres' \
                       host='localhost' password='xxxxxxxx'")
    print('Connected to DB')

    cur = conn.cursor()
    cur.execute("SELECT 'Track_Length' FROM t_tracks \
                     WHERE 'Track_ID' = 'Daytona';")

    length = cur.fetchone()
    print('Track Length = ', length)

    conn.close()

问题在于
轨道长度
轨道ID
周围的引号。由于使用引号,列名当前被解释为实际字符串,导致字符串
Track_ID
Daytona
之间的比较不令人满意。这将导致0行与查询匹配,并
length
获取
None

删除列名周围的单引号:

cur.execute("""
    SELECT Track_Length 
    FROM t_tracks
    WHERE Track_ID = 'Daytona'
""")

或者在表名周围使用双引号。