Python Forum
Problems with save array into Mysql
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problems with save array into Mysql
#1
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
Reply
#2
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]]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to save to multiple locations during save cubangt 1 579 Oct-23-2023, 10:16 PM
Last Post: deanhystad
  Mysql and mysql.connector error lostintime 2 719 Oct-03-2023, 10:25 PM
Last Post: lostintime
  Mysql error message: Lost connection to MySQL server during query tomtom 6 16,220 Feb-09-2022, 09:55 AM
Last Post: ibreeden
Question Debian 11 Bullseye | Python 3.9.x | pip install mysql-connector-python-rf problems BrandonKastning 4 6,743 Feb-05-2022, 08:25 PM
Last Post: BrandonKastning
  SaltStack: MySQL returner save less data into Database table columns xtc14 2 2,199 Jul-02-2021, 02:19 PM
Last Post: xtc14
  Save Numpy Array in Another Numpy Array (not Concatenate!) quest 1 1,879 Nov-06-2020, 01:06 PM
Last Post: quest
  Save Arduino data in mysql server via raspberrypi rithikvg 1 3,440 Jun-24-2020, 10:59 PM
Last Post: Larz60+
  Trying to save array results to a csv jos88 0 1,623 Sep-08-2019, 08:01 PM
Last Post: jos88
  Small help for the python array save juniorcoder 1 2,195 Sep-27-2018, 09:28 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020