Python:光标“NoneType”对象没有属性

Alp*_*001 0 python

我正在尝试使用游标来玩这个简单的 sql 查询:

import jaydebeapi,pandas as pd

conn = <connection to TeraData>
cursor = conn.cursor()
sql = ("Select top 10 * from Car_Sales")
data = cursor.execute(sql)
print(data.query)
row = data.fetchone()
#rows = data.fetchall()
print(row)
Run Code Online (Sandbox Code Playgroud)

我得到的错误是:

AttributeError:“NoneType”对象没有属性“fetchone”

我们花了几个小时试图找到问题,但没有取得任何进展。在网上查看了不同的资源,但问题仍然存在。

请让我知道我哪里出错了?

更新:

当我在 Pandas 中执行相同的查询时:

pd.read_sql_query('select top 10 * from  Car_Sales',conn)
Run Code Online (Sandbox Code Playgroud)

输出:

Car_Sales
0 112
1 77
2 226
3 990
4 552
5 887
Run Code Online (Sandbox Code Playgroud)

chr*_*ris 5

您还没有告诉我们您正在使用哪个连接库,但在我熟悉的情况下(psycopg等)cursor.execute()不会返回结果集。尝试针对(而不是)发出fetchone和命令:fetchallcursordata

conn = <connection to TeraData>
cursor = conn.cursor()
cursor.execute("Select top 10 * from Car_Sales")

row = cursor.fetchone()
print(row)

# rows = cursor.fetchall()   
# print(rows)
Run Code Online (Sandbox Code Playgroud)

更新

Blckknght 评论中的一个花絮 - 查看Python 数据库 API 规范:“返回值未定义” cursor.execute