Home > Back-end >  ValueError: not enough values to unpack (expected 2, got 0) Python and tkinter and Mysql
ValueError: not enough values to unpack (expected 2, got 0) Python and tkinter and Mysql

Time:11-21

I need help. I have a patient_results_tbl as shown below and I want to retrieve voltage and time based on the patient_id and date, exact much.

# patient_id,    voltage,     time,          date
'260939631',     '329',       '18:34:37',    '2021-11-05'
'260939631',     '334',       '18:34:39',    '2021-11-05'
'260939631',     '352',       '18:34:39',    '2021-11-05'
'260939631',     '341',       '18:51:01',    '2021-11-13'
'260939631',     '338',       '18:51:02',    '2021-11-13'
'260939631',     '331',       '18:51:03',    '2021-11-13'
'260939631',     '324',       '18:51:04',    '2021-11-13'
'260939631',     '319',       '18:51:05',    '2021-11-13'
'785393661',     '274',       '18:58:07',    '2021-11-13'
'785393661',     '280',       '18:58:08',    '2021-11-13'
'785393661',     '285',       '18:58:08',    '2021-11-13'
'785393661',     '290',       '18:58:09',    '2021-11-13'

The code am using is as follows:

def analyze_voltage_time():
            start_point = int(start_point_entry.get()) # getting from entry box
            end_point = int(end_point_entry.get())  # getting from entry box
            step = int(step_point_entry.get())  # getting from entry box
            start = get_date2()  # getting from tkcalender date picker
            _id = id_selector.get() # # getting from combobox
            pat_id = _id[:9]
            #query = "SELECT voltage, time FROM patient_results_tbl where patient_id =  "  pat_id
            #mycursor.execute(query, )
            query = "SELECT voltage, time FROM patient_results_tbl WHERE 'date' =%s AND 'patient_id' =%s"
            mycursor.execute(query, (start, pat_id))
            result = mycursor .fetchall()
            print(result)  # For checking 
            print(start)     # For checking purpose 
            print(pat_id)   # For checking purpose
            voltage, time = list(zip(*result))  # ValueError: not enough values to unpack (expected 2, got 0)
            for volts in voltage:
                voltage_container.append(volts)
            for tim in time:
                time_container.append(str(tim))
            try:
                user_data = range(start_point, end_point, step)
                for i in user_data:
                    plt_voltage.append(voltage_container[i])
                    plt_time.append(time_container[i])
    
            except ValueError:
                user_data = range(start_point, end_point)
                for i in user_data:
                    plt_voltage.append(voltage_container[i])
                    plt_time.append(time_container[i])
                
            my_ax1.plot(plt_time, plt_voltage, color="red")        
            my_canvas1.draw_idle()
            analyse_btn['state'] = DISABLED

Running the above code as a standalone app gives me the following error:

[]
05/11/2021
260939631
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\Kennedy Mulenga\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1892, in __call__
    return self.func(*args)
  File "C:\Users\Kennedy Mulenga\Desktop\Level 5\18136709_BIT_280_Arduino_ECG_Project\18136709\18136709_ECG_LArdmon_APP_Final.py", line 418, in <lambda>
    analyse_btn = Button( tools_frame, text='Analyse', width = 8, height = 2, command = lambda :[get_date2(), analyze_voltage_time()])
  File "C:\Users\Kennedy Mulenga\Desktop\Level 5\18136709_BIT_280_Arduino_ECG_Project\18136709\18136709_ECG_LArdmon_APP_Final.py", line 285, in analyze_voltage_time
    voltage, time = list(zip(*result))
ValueError: not enough values to unpack (expected 2, got 0)

If I run with the commented out query it returns all the records with respect to the patient_id which is ok. And also running the query SELECT voltage, time FROM patient_results_tbl WHERE date = 13/11/2021 AND patient_id = 785393661 directly in Mysql workbench return 0 rows, but if I replace AND with OR am getting some results

# patient_id    voltage    time       date
785393661       274        18:58:07   2021-11-13
785393661       280        18:58:08   2021-11-13
785393661       285        18:58:08   2021-11-13
785393661       290        18:58:09   2021-11-13

I don't know where am not doing well, or is there a way to do it?

CodePudding user response:

You should provide your code as a minimal, reproducible example, it's easier to try and fix if we can copy paste and get the same error immediately.

The date you input into the query is in the format "dd/mm/yyyy" but your table has it in the format "yyyy-mm-dd". Therefore your results ends up as an empty list, which you can see as it's printed above your traceback, which is giving you the ValueError.

CodePudding user response:

I have replaced the line :

query = "SELECT voltage, time FROM patient_results_tbl WHERE 'date' =%s AND 'patient_id' =%s"
            mycursor.execute(query, (start, pat_id))

with:

mycursor.execute("SELECT voltage, time FROM patient_results_tbl WHERE date =%s AND patient_id =%s", (start, pat_id))

by removing the quotes around date and pat_id which has worked now, and if there is a better way to do it I will appreciate it and thanks.

  • Related