mysql store numpy array 1

mysql store numpy array

myNumpyArray = np.random.rand((3,3))

#convert to list, then to string, then store as VARCHAR(20000) in mysql
numpyString = str(myNumpyArray.tolist()) 
query = "insert into mytable(mycolumn) VAlUES ('"+numpyString+"')"
cursor.execute(query)

#Note: to retreive from mysql,you can convert to array using eval:
#myArray = eval(mycolumn)

Here is what the above code is Doing:
1. Create a numpy array
2. Convert the numpy array to a list
3. Convert the list to a string
4. Store the string in a VARCHAR(20000) column in mysql

To retrieve the array from mysql, you can use eval:
myArray = eval(mycolumn)

Similar Posts