Python Forum
Problems with save array into Mysql - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Problems with save array into Mysql (/thread-21372.html)



Problems with save array into Mysql - storzo - Sep-26-2019

Hi !
I don't know how to upload my array in numpy to msql:
arr=np.zeros((24,7),dtype=object) 
b = bytes(arr)
#print value of: b'\xe0p\x08\xdc\xfe\...'

...
do_it = """UPDATE db_db.bytedata 
                                SET bDATA=%s 
                                 WHERE id =%s"""

        mycursor.execute(do_it, (b,id,))
In mysql i setup colum value as BLOB but i got:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe0 in position 0: invalid continuation byte

When i setup value in text (i know is wrong) i got record:
('?p???\x07\x00\x0... <- so its wrong Hand

i think i must use VARBINARY value in Mysql column but i dont now how prepare data in python to send in corect format and utf. Huh


RE: Problems with save array into Mysql - storzo - Sep-27-2019

I found solution for everyone here it is:

        def array2str(arry, precision=None):
            s = np.array_str(arry, precision=precision)
            s= s.replace('\n', '')
            s= s.replace(']', '')
            #s = s.replace(' ', ',')
            return s.replace('[', '')
def dissolve all things and we get something like this:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
we can send that to msql for text with utf-8
when we take txt we can make list in first step:
change_to_list=list(map(int, change_to_list.split()))
then reshape : np.reshape(change_to_list, (x,y))
[[ 0  0  0  0  0  0  0]
 [ 0  0  0  0  0  0  0]
 [ 0  0  0  0  0  0  0]
 [ 0  0  0  0  0  0  0]]