9951 explained code solutions for 126 technologies


python-mysqlHow can I use Python and MySQL to bind named parameters in a query?


Using Python and MySQL to bind named parameters in a query is a great way to ensure that your queries are secure and efficient. To do this, you need to use the MySQL Connector/Python library.

Here is an example of how to use named parameters in a query:

import mysql.connector

# Create a connection object
mydb = mysql.connector.connect(
    host="localhost",
    user="user",
    passwd="password"
)

# Create a cursor object
my_cursor = mydb.cursor()

# Define the query
my_query = "SELECT * FROM users WHERE name = %(name)s"

# Define the parameters
my_params = { "name": "John" }

# Execute the query
my_cursor.execute(my_query, my_params)

# Fetch the result
result = my_cursor.fetchall()

# Print the result
print(result)

Output example

[(1, 'John', 'Doe', '[email protected]'), (2, 'John', 'Smith', '[email protected]')]

The code above does the following:

  1. Imports the mysql.connector library.
  2. Creates a connection object.
  3. Creates a cursor object.
  4. Defines the query with a named parameter (%(name)s).
  5. Defines the parameters.
  6. Executes the query with the parameters.
  7. Fetches the result.
  8. Prints the result.

For more information, please refer to the MySQL Connector/Python documentation.

Edit this code on GitHub