Python:游标'NoneType'对象没有属性

时间:2018-08-31 00:30:21

标签: 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)

我遇到的错误是:

AttributeError:'NoneType'对象没有属性'fetchone'

已经花了几个小时才找到问题,但没有解决任何问题。在网上看了不同的资源,但问题仍然存在。

请让我知道我要去哪里错了?

更新:

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

pd.read_sql_query('select top 10 * from  Car_Sales',conn)

输出:

Car_Sales
0 112
1 77
2 226
3 990
4 552
5 887

1 个答案:

答案 0 :(得分:3)

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

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)

更新

来自Blckknght的评论的花絮-看一下Python Database API Speccursor.execute的“未定义返回值”。

相关问题