Python to mysql database

Hi everybody.
I search the right way to export a data from the autopi to mysql database. Because I have already a web app ready to work with.
It is for EV car, for now I just need the SoC (state of charge)
@Plord seem find the command who is something like that:
obd.query mode=220 pid=101 force=true
Then if the dongle answer, I want put the data to mysql.
So, I try to write in the autopi cloud this:
“my evdat script”
import serial
import MySQLdb
import re
import time
import string
import io

db = MySQLdb.connect(host=“External-IP :3307”, # your host, usually localhost
user=“user”, # your username
passwd=“Password”, # your password
db=“kona”) # name of the data base

cur = db.cursor()

ser = serial.Serial("/dev/rfcomm0", timeout=None)
ser.baudrate = 9600
ser.flushInput()
ser.write(b’2105\r\n’)
ser.flush()
seq = []
while True:
reading = ser.read()
seq.append(reading)
joineddata = ’ ‘.join(str(v) for v in seq).replace(’ ', ‘’)

err = re.search('ERROR', joineddata)
if err:
    break
m = re.search('4([^;]*)5:', joineddata) #'/4([^;]*)\n5', joineddata)
if m:
    ser.close()
    test = str(m.group(0))
    x = (test[-8:])      
    SoC = (int( x[3:5], 16)/2)
    if SoC > 0 & SoC <= 100:
        print(SoC)
        cur.execute("""INSERT INTO bms (soc) VALUES (%s)""", (SoC,) )
        db.commit()
    break

image

I don’t know if it is the right way to do it

Anyone know ?

Dear @Remy_Tsuihiji
May I know you got the solution? and which type of custom code type did you use?