Why would MySQL execute return None?

后端 未结 1 1068
感动是毒
感动是毒 2020-12-20 12:33

I am trying to query on a local MySQL database using Python\'s (3.4) MySQL module with the following code:

class databases():

  def externaldatabase(self):
         


        
相关标签:
1条回答
  • 2020-12-20 13:18

    Query executions have no return values.

    The pattern you need to follow is:

    cursor creation;
    cursor, execute query;
    cursor, *fetch rows*;
    

    Or in python:

    c = d.cursor()
    
    c.execute(query)    # selected rows stored in cursor memory
    
    rows = c.fetchall()    # get all selected rows, as Barmar mentioned
    for r in rows:
        print(r)
    

    Also some db modules allow you to iterate over the cursor using the for...in pattern, but triple-check that regarding mysql.

    0 讨论(0)
提交回复
热议问题