9951 explained code solutions for 126 technologies


python-mysqlHow can I retrieve a value from a MySQL database using Python?


To retrieve a value from a MySQL database using Python, you can use the MySQL Connector/Python library. The following example code will connect to a MySQL database, execute a query, and store the result in a variable:

import mysql.connector

# Connect to the database
db = mysql.connector.connect(
    host="localhost",
    user="user",
    passwd="password",
    database="database_name"
)

# Create a cursor object
cursor = db.cursor()

# Execute a query
query = "SELECT * FROM table_name"
cursor.execute(query)

# Store the result in a variable
result = cursor.fetchone()

The cursor.fetchone() method will return the first row of the result set as a tuple, which can be stored in the result variable.

The code consists of the following parts:

  1. import mysql.connector: This imports the MySQL Connector/Python library.
  2. db = mysql.connector.connect(): This connects to the MySQL database using the specified credentials.
  3. cursor = db.cursor(): This creates a cursor object which can be used to execute queries.
  4. query = "SELECT * FROM table_name": This defines the query which will be executed.
  5. cursor.execute(query): This executes the query.
  6. result = cursor.fetchone(): This stores the first row of the result set in the result variable.

For more information, see the MySQL Connector/Python documentation.

Edit this code on GitHub