How to insert / retrieve a file stored as a BLOB in a MySQL db using python

Question:

I want to write a python script that populates a database with some information. One of the columns in my table is a BLOB that I would like to save a file to for each entry.

How can I read the file (binary) and insert it into the DB using python? Likewise, how can I retrieve it and write that file back to some arbitrary location on the hard drive?

Asked By: RyanY

||

Answers:

You can insert and read BLOBs from a DB like every other column type. From the database API’s view there is nothing special about BLOBs.

Answered By: unbeknown
thedata = open('thefile', 'rb').read()
sql = "INSERT INTO sometable (theblobcolumn) VALUES (%s)"
cursor.execute(sql, (thedata,))

That code of course works as written only if your table has just the BLOB column and what
you want to do is INSERT, but of course you could easily tweak it to add more columns,
use UPDATE instead of INSERT, or whatever it is that you exactly need to do.

I’m also assuming your file is binary rather than text, etc; again, if my guesses are
incorrect it’s easy for you to tweak the above code accordingly.

Some kind of SELECT on cursor.execute, then some kind of fetching from the cursor, is how you
retrieve BLOB data, exactly like you retrieve any other kind of data.

Answered By: Alex Martelli

i faced a lot of problems in using BLOB and i solved these problems. the solution is below :

now there are a lot of problems when you send bytes of data into the database using the method or the way that is above .. why ?

because when you read image or file as bytes will contain a lot of quotes in it single and double , and when you chane the byte to string and fix this thing , the bytes will be wrong and unaccepted when you import it from the database.

then … how can i solve this problem ?

to solve this problem you will change the bytes to HEX and save it in the database after thet when you want to import it you will import it as HEX and change it to Bytes to handle it in your code.

but there is an additional error : when you import the hex from BLOB it will be HEX as bytes, then how can we solve that ? we will use LONGTEXT instead of LONGBLOB.

here is an example:


import mysql.connector


file = "pipc.png"


open_the_file = open(file , "rb")

the_file_as_bytes = open_the_file.read()

convert_the_bytes_to_hex = bytes(the_file_as_bytes).hex()


connection = mysql.connector.connect(host="localhost",user="yourusername",password="yourpassword")
my_cursor = connection.cursor()
creating_database = my_cursor.execute("CREATE DATABASE IF NOT EXISTS example;")
using_database = my_cursor.execute("USE example;")
creating_table = my_cursor.execute("CREATE TABLE IF NOT EXISTS example(file LONGTEXT);")


adding_command = my_cursor.execute("INSERT INTO example(file) VALUES ('{}');".format(convert_the_bytes_to_hex))

#         note that you are inserting the hex code in quotes           ^^^


importing_command = my_cursor.execute("SELECT file FROM example;")


# converting the hex code to bytes

for i in my_cursor.fetchall():
    print(bytes().fromhex(i[0]))

Answered By: Mohammed almalki
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.