If you encounter a problem that you cannot normally save a numpy array into a MySQL database , then this post is for you! The original post was posted on my blog .I chose a save method for myself through the pickle module . With it, you can safely save a numpy array of any dimension in the blob of the MySQL database.So, an example code:import mysql.connector
import pickle
import numpy as np
connection = mysql.connector.connect(host='localhost',
database='database',
user='root',
password='')
cursor = connection.cursor()
query = 'create table test(`column` blob);'
cursor.execute(query)
array = np.array([[1,2,3],[4,5,6]])
query = 'insert into test values(%s);'
db_array = pickle.dumps(array)
cursor.execute(query, [db_array ])
query = 'select * from test;'
cursor.execute(query)
res = cursor.fetchall()
res
pickle.loads(res[0][0])
array([[1, 2, 3],
[4, 5, 6]])
cursor.close()
connection.close()
In this way, you can exchange numpy multidimensional (and regular) numpy arrays between Python and MySQL in an absolutely unhindered fashion. If you read the image using cv2.imread from the OpenCV library, for example, then the principle remains the same - after all, it is, in fact, a multidimensional array.I hope this helps someone to solve a similar problem!