9951 explained code solutions for 126 technologies


python-mysqlHow can I use Python to insert binary data into a MySQL database?


Using Python to insert binary data into a MySQL database is a simple process. To accomplish this, you need to use the MySQL Connector/Python library. The following example code demonstrates how to do this:

import mysql.connector

# Connect to the database
db = mysql.connector.connect(host='localhost',
                             user='user',
                             passwd='passwd',
                             database='mydb')

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

# Insert binary data into the database
sql = "INSERT INTO mytable (binary_data) VALUES (%s)"
data = (b'\x01\x02\x03\x04', )
cursor.execute(sql, data)

# Commit the changes to the database
db.commit()

# Close the connection
db.close()

The code above will insert the binary data \x01\x02\x03\x04 into the mytable table in the mydb database.

The code can be broken down into the following parts:

  1. Import the mysql.connector library.
  2. Establish a connection to the database.
  3. Create a cursor object.
  4. Create an SQL statement to insert the binary data into the database.
  5. Execute the SQL statement with the binary data as a parameter.
  6. Commit the changes to the database.
  7. Close the connection.

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

Edit this code on GitHub