ValueError: no enough values to unpack (expected 2, got 0) Python and tkinter and Mysql
Posted
技术标签:
【中文标题】ValueError: no 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 【发布时间】:2021-12-31 01:35:12 【问题描述】:我需要帮助。我有一个patient_results_tbl
,如下所示,我想根据patient_id
和date
检索voltage
和time
,非常多。
# 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'
我使用的代码如下:
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
将上述代码作为独立应用运行时会出现以下错误:
[]
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)
如果我使用已注释掉的查询运行,它会返回与 patient_id 相关的所有记录,这没问题。
并且还直接在mysql
工作台中运行查询SELECT voltage, time FROM patient_results_tbl WHERE date = 13/11/2021 AND patient_id = 785393661
返回0 rows
,但如果我用OR
替换AND
我会得到一些结果
# 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
不知道哪里做得不好,或者有什么办法?
【问题讨论】:
【参考方案1】:您应该以minimal, reproducible example 的形式提供您的代码,如果我们可以复制粘贴并立即得到相同的错误,则更容易尝试和修复。
您在查询中输入的日期格式为“dd/mm/yyyy”,但您的表格的格式为“yyyy-mm-dd”。因此,您的结果最终成为一个空列表,您可以看到它打印在您的回溯上方,这给了您ValueError
。
【讨论】:
当插入格式接受的数据库时,但我已将其更改为适合数据库显示的内容,并且我找到了一种可行的方法。 @Kennerdol 道歉,没有实际数据库很难找到问题【参考方案2】:我已经换了行:
query = "SELECT voltage, time FROM patient_results_tbl WHERE 'date' =%s AND 'patient_id' =%s"
mycursor.execute(query, (start, pat_id))
与:
mycursor.execute("SELECT voltage, time FROM patient_results_tbl WHERE date =%s AND patient_id =%s", (start, pat_id))
通过删除 date
和 pat_id
周围的引号
现在已经奏效了,如果有更好的方法,我将不胜感激并感谢。
【讨论】:
以上是关于ValueError: no enough values to unpack (expected 2, got 0) Python and tkinter and Mysql的主要内容,如果未能解决你的问题,请参考以下文章
ValueError: no enough values to unpack (expected 2, got 0) Python and tkinter and Mysql
如何处理 scipy minimize ValueError: no enough values to unpack (expected 4, got 3)?
celery 启用worker ValueError: not enough values to unpack
Celery ValueError: not enough values to unpack (expected 3, got 0)的解决方案
python调用cv2.findContours时报错:ValueError: not enough values to unpack (expected 3, got 2)
celery接收到任务后出现错误,ValueError: not enough values to unpack (expected 3, got 0)