0

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
1
  • 2
    This means that data is None. Maybe your SQL query is invalid? "Select top 10 * Car_Sales" doesn't look like a correct query to me Commented Aug 31, 2018 at 0:31

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Indeed, this is part of the Python DB API which most libraries try to follow.
@chris Apologies I am using Jaydebeapi library. Have updated the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.