【问题标题】:Retrieving values from mysql database and saving them to an empty array with python从mysql数据库中检索值并使用python将它们保存到一个空数组中
【发布时间】:2021-12-13 23:55:35
【问题描述】:

我有一个 MySQL 数据库(使用 MySQL 工作台),它有两个表(ecg_pat_tbl 和 ecg_data_tbl),并将浮点值、日期和时间分别发送到电压、日期和时间列,并且同时绘制图表(电压对时间),一切顺利,现在我想检索电压和时间值,将它们保存到不同的数组中,以便我可以使用它们进行分析,我该如何检索和保存它们? 下面的代码是用于绘制并保存到数据库的函数。

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")

感谢所有回复。

【问题讨论】:

    标签: mysql python-3.x user-interface


    【解决方案1】:

    如果我错了,请纠正我,但您唯一的问题似乎是从格式光标的返回值中解压缩值:(time_0, volt_0), (time_1, volt_1), ...。代码应如下所示。

    
    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, ...)
    

    【讨论】:

      猜你喜欢
      • 2020-08-02
      • 1970-01-01
      • 2021-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多