Working with MySQL BLOB in Python
In Python Programming, We can connect with several databases like MySQL, Oracle, SQLite, etc., using inbuilt support. We have separate modules for each database. We can use SQL Language as a mediator between the python program and database. We will write all queries in our python program and send those commands to the database. So, Using these programs, we can perform several operations such as Insertion, Deletion, Updating, and Retrieving.
Here, In this article, We will discuss working with MySQL BLOB in python. With the help of BLOB(Large Binary Object) data type in MySQL, we can store files or images in our database in binary format.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
Installation of MySQL Connector:
This connector will connect our python program to database. Just run this command,
pip install mysql-connector-python
Important steps for Python Database Programming:
- Import MySQL database Module
- For creating a connection between Python Program and Database. Using connect() method, We will connect the python program with our database.
connection = mysql.connector.connect(host=’localhost’, database='<database_name>’, user='<User_name>’, password='<password>’)
- Now, create a cursor object by using cursor() method for executing the SQL Queries and holding the result in an object.
cursor = connection.cursor()
- For executing SQL queries, we will use a cursor object. For example,
cursor.execute("select * from table_name")
- Finally, Once we are done with our operations, we have to close the resources.
We are done with the basic steps of connection. Now, Let’s discuss the main agenda of this article which is the practical implementation of BLOB data type in MySQL Python,
- First, We need to create a database in MySQL using the below command.
create database geeksforgeeks;
- Creating a function through which we can convert images or files in binary format.
- Check Whether Database Connection is created or not using Python Program. Let’s have a look in below code:
We are done with all basic which is required. Let’s see the complete code for inserting the images or files in the MySQL database using Python Programs:
The table formed in MySQL:
- Establishing the connection with MySQL database.
- Write the create table Query and Using cursor object, Executing it.
- Now, Insert data into a table using SQL query and stored in query variable.
- Storing the data in variables such as student_id = “1”, Student_name = “Shubham” and for images or files, first we are converting those files into binary data and then stored into a variables.
- Using cursor object, Executing the query. Inserting the data in the database in tuple format.
- Using commit() method, We are saving the data.
- After completing all operations, we have to close all the resources such as the connection and cursor object.