I have a MySQL DB (using MySQL workbench) having two tables (ecg_pat_tbl & ecg_data_tbl), and sending float values, Date and Time to columns voltage, date, and time respectively, and am also plotting a graph at the same time (voltage against time), everything is going well, now I want to retrieve both voltage and time values save them to different arrays so that I can use them for analysis, how do I go about retrieving and saving them? The code below is the function am using to plot and save to the DB.
data =[]
_condition = False
amount = 0
def graphing():
global _condition, data
if (_condition == True):
try:
identity = identity_var.get() # Getting patient ID
date_time=datetime.datetime.now()
my_time='{}:{}:{}'.format(date_time.hour,date_time.minute,date_time.second)
my_date='{}/{}/{}'.format(date_time.year, date_time.month,date_time.day)
serial_data = _serial.readline()
serial_data.decode()
float_data = float(serial_data[0:4])
query = "INSERT INTO ecg_data_tbl(patient_id, voltage, date, time) VALUES(%s, %s, %s, %s)"
mycursor.execute(query, (identity, float_data, my_date, my_time))
mycursor.execute("commit")
data.append(float_data)
ax.plot(data, color="blue")
canvas.draw_idle()
except ValueError as e:
_condition = False
start_button['state'] = NORMAL
freeze_button['state'] = DISABLED
save_button['state'] = DISABLED
insert_button['state'] = DISABLED
messagebox.showinfo("Connection", "Check connnection of the patient cable")
all responses are appreciated.
CodePudding user response:
Correct me if I'm wrong, but it seems your only issue is unzipping the values from the format cursor's return value: (time_0, volt_0), (time_1, volt_1), ...
. The code should look something like this.
query = "SELECT time, voltage FROM ecg_data_tbl WHERE patient_id=?"
result = cursor.execute(query, (identity,)).fetchall()
# (time_0, volt_0), (time_1, volt_1), ...
times, volts = list(zip(*result))
# (time_0, time_1, ...), (volt_0, volt_1, ...)