9951 explained code solutions for 126 technologies


python-mysqlHow can I use multiple cursors in Python to interact with MySQL?


Using multiple cursors in Python to interact with MySQL can be done with the MySQLdb library. Below is an example of how to use multiple cursors in Python to interact with MySQL:

import MySQLdb

# Create connection to MySQL
conn = MySQLdb.connect(host='localhost', user='root', password='password', db='test')

# Create two cursors
cursor1 = conn.cursor()
cursor2 = conn.cursor()

# Execute SQL query
cursor1.execute("SELECT * FROM table1")
cursor2.execute("SELECT * FROM table2")

# Fetch data from cursors
data1 = cursor1.fetchall()
data2 = cursor2.fetchall()

# Print data
print(data1)
print(data2)

# Close connection to MySQL
conn.close()

Output example

[('row1', 'data1'), ('row2', 'data2'), ...]
[('row1', 'data1'), ('row2', 'data2'), ...]

The code above does the following:

  1. Imports the MySQLdb library.
  2. Creates a connection to the MySQL server.
  3. Creates two cursors.
  4. Executes two SQL queries.
  5. Fetches the data from the two cursors.
  6. Prints the data.
  7. Closes the connection to the MySQL server.

Helpful links

Edit this code on GitHub