Why is this command not working in Python when attempting to display a MySql database

cursor = conn.cursor(dictionary=True)
show_table = """
SELECT 
itemdescription, 
quantity, 
dateadded
FROM 
shoppinglistTest2;
"""
execute_query(conn, show_table)
cursor.execute(show_table)
rows = cursor.fetchall()
for user in rows:
   print(user)

I am using python to connect to a MySql database and display the tables with these attributes but I get this error after the table is created and data is already there

The error 'Unread result found' occurred
Traceback (most recent call last):
  File "/Users/rhythm/Documents/CIS 3368/Shopping test 2", line 29, in <module>
    cursor.execute(show_table)

UPDATE:
I found that if I just add an item to the list then I get the list printed

item_des = 'Pie'
item_quant = 2
item_added = datetime.date.today()
query = "INSERT INTO shoppinglistTest2 (itemdescription, quantity, dateadded) VALUES ('%s', %s, '%s')" % (item_des,     item_quant, item_added)
execute_query(conn, query)
cursor = conn.cursor(dictionary=True)
run = """
SELECT 
itemdescription, 
quantity, 
dateadded
FROM 
shoppinglistTest2;
"""
cursor.execute(run)
rows = cursor.fetchall()
for user in rows:
    print(user)

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Have you tried the following:

cursor = conn.cursor()
show_table = """
SELECT 
itemdescription, 
quantity, 
dateadded
FROM 
shoppinglistTest2;
"""
cursor.execute(show_table)
for itemdescription, quantity, dateaddedin in cursor.fetchall():
   print(itemdescription)
   print(quantity)
   print(dateaddedin )

Method 2

The problem is you call execute_query() before cursor.execute() without reading the results from the query, which MySQL connector does not like


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x