1

enter image description here

I have a problem getting the query results from my Python-Code. The connection to the database seems to work, but i always get the error:

"InterfaceError: No result set to fetch from."

Can somebody help me with my problem? Thank you!!!

cnx = mysql.connector.connect(
    host="127.0.0.1" , 
    user="root" , 
    passwd="*****",
    db="testdb"
)
cursor = cnx.cursor()
query = ("Select * from employee ;")

cursor.execute(query)

row = cursor.fetchall()
Azat Ibrakov
  • 9,998
  • 9
  • 38
  • 50
D. P.
  • 11
  • 1
  • 2

3 Answers3

1

If your problem is still not solved, you can consider replacing the python mysql driver package and use pymysql. You can write code like this

#!/usr/bin/python
import  pymysql

db = pymysql.connect(host="localhost",    # your host, usually localhost
                     user="test",         # your username
                     passwd="test",  # your password
                     db="test")        # name of the data base

# you must create a Cursor object. It will let
#  you execute all the queries you need
cur = db.cursor()

query = ("SELECT * FROM employee")

# Use all the SQL you like
cur.execute(query)

# print all the first cell of all the rows
for row in cur.fetchall():
    print(row[0])

db.close()

This should be able to find the result you want

KadyCui
  • 11
  • 2
  • This seems to have worked. I would recommend anyone using python to use this driver instead as the base one suffers from lack of maintenance – AzyCrw4282 Apr 26 '22 at 14:51
-1

add this to your code

for i in row:
    print(i)

you did not print anything which is why that's not working this will print each row in separate line

  • 2
    While this code may answer the question, it would be better to include some context, explaining how it works and when to use it. Code-only answers are not useful in the long run. – Mustafa Apr 20 '20 at 04:08
-1

first try to print(row),if it fails try to execute using the for the loop,remove the semicolon in the select query statement

cursor = connection.cursor()
rows = cursor.execute('SELECT * FROM [DBname].[dbo].TableName where update_status is null ').fetchall()

for row in rows:
    ds = row[0]
    state = row[1]

here row[0] represent the first columnname in the database & row[1] represent the second columnname in the database & so on