简体   繁体   中英

Python: Cursor 'NoneType' object has no attribute

I am trying to play around with this simple sql query using cursor:

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'

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:

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. Try issuing the fetchone and fetchall commands against the cursor (rather than data ):

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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