9951 explained code solutions for 126 technologies


python-mysqlHow do I use Python to fetch an associative array from a MySQL database?


To fetch an associative array from a MySQL database using Python, you need to use the Python MySQL Connector library. Here is an example of code that you can use:

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="yourusername",
  passwd="yourpassword"
)

mycursor = mydb.cursor(dictionary=True)

mycursor.execute("SELECT * FROM customers")

myresult = mycursor.fetchall()

for x in myresult:
  print(x)

The output of this code would be a list of associative arrays, each containing the data for a single customer from the customers table.

Code explanation

  1. import mysql.connector: This imports the MySQL Connector library.
  2. mydb = mysql.connector.connect(host="localhost", user="yourusername", passwd="yourpassword"): This connects to the MySQL database using the specified username and password.
  3. mycursor = mydb.cursor(dictionary=True): This creates a cursor object with the dictionary flag set to True, which will return the results as an associative array.
  4. mycursor.execute("SELECT * FROM customers"): This executes the query on the database, which in this case is selecting all records from the customers table.
  5. myresult = mycursor.fetchall(): This fetches all of the results from the query and stores them in the myresult variable.
  6. for x in myresult: print(x): This iterates through the myresult variable and prints out each result.

Here are some useful links for further reading:

Edit this code on GitHub