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

Time:10-02

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)

CodePudding user response:

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

CodePudding user response:

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 )

  • Related