0

note I am using the following version of Python on a Windows OS:

(venv) C:\>python
    Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 16:07:46) [MSC v.1900 32 bit (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.

*The following Python script below is run and the output is shown directory below it. Most of this script has been used from the following source:

https://pynative.com/python-mysql-select-query-to-fetch-data/

SCRIPT 1

import mysql.connector
from mysql.connector import Error
try:
    cnx = mysql.connector.connect(user='root', password='coldplay123',
                                  host='localhost',
                                  database='nathan_test_1')

    cursor_1 = cnx.cursor()
    s1="select * from dataframe"
    cursor_1.execute(s1)
    data1 = cursor_1.fetchall()

    print("Total number of dataframes: ", cursor_1.rowcount)

    for i1 in data1:
        print(i1)
    cursor_1.close()

except Error as e1:
    print("Failure to connect ... ", e1)

cnx.close()

Output of SCRIPT 1

Total number of dataframes:  1
('a', 'b', 'c', 699)

*Now, I change just two lines from Script 1 in the middle by simply commenting them out and the following output is generated:

SCRIPT 2

import mysql.connector
from mysql.connector import Error
try:
    cnx = mysql.connector.connect(user='root', password='coldplay123',
                                  host='localhost',
                                  database='nathan_test_1')

    cursor_1 = cnx.cursor()
    #s1="select * from nathan"
    #cursor_1.execute(s1)
    data1 = cursor_1.fetchall()

    print("Total number of dataframes: ", cursor_1.rowcount)

    for i1 in data1:
        print(i1)
    cursor_1.close()

except Error as e1:
    print("Failure to connect ... ", e1)

cnx.close()

Output of SCRIPT 2

Failure to connect ...  No result set to fetch from.

*It is easy to see what causes this error, but why does not allowing 'cursor_1' to execute a given SQL query cause this error?

1 Answer 1

1

According to PEP 249 - Python Database API Specification v2.0 , fetchone, fetchmany, fetchall documentation,

An Error (or subclass) exception is raised if the previous call to .execute*() did not produce any result set or no call was issued yet.

fetch*() call should be followed by execute call.

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

Comments

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.