简体   繁体   English

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

[英]Python: Cursor 'NoneType' object has no attribute

I am trying to play around with this simple sql query using cursor: 我正在尝试使用游标玩这个简单的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)

Error I am getting is: 我得到的错误是:

AttributeError: 'NoneType' object has no attribute 'fetchone' AttributeError:“ NoneType”对象没有属性“ fetchone”

It has been couple hours trying to find the issue but not getting anywhere on this. 试图找到问题已经花费了几个小时,但未能解决任何问题。 Looked different resources online but still the issue remains. 在网上查找了其他资源,但问题仍然存在。

Please let me know where am I going wrong? 请让我知道我要去哪里错了?

Update: 更新:

When I executed the same query in Pandas: 当我在Pandas中执行相同的查询时:

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

Output: 输出:

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

You haven't told us which connection library you're using, but in cases I'm familiar with ( psycopg , etc) cursor.execute() doesn't return a result set. 您没有告诉我们您正在使用哪个连接库,但是在我熟悉的情况下( psycopg等), cursor.execute()不会返回结果集。 Try issuing the fetchone and fetchall commands against the cursor (rather than data ): 尝试针对cursor (而不是data )发出fetchonefetchall命令:

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)

Update 更新

A tidbit from Blckknght's comment - take a look at the Python Database API Spec : "Return values are not defined" for cursor.execute . Blckknght的评论的花絮-看一下Python数据库API规范cursor.execute “未定义返回值”。

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

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