gpt4 book ai didi

python - 使用Python更新mysql错误

转载 作者:行者123 更新时间:2023-11-29 19:32:38 26 4
gpt4 key购买 nike

这是我的情况:我在数据库上得到了一张表,如下所示:

nome_imagem                       estado     type
57260-tracker-_tracker_face awake 0
57261-tracker-_tracker_face drowsiness 1
57268-tracker-_tracker_face noface 2
57289-tracker-_tracker_face distracted 3
57290-tracker-_tracker_face awake 1
57291-tracker-_tracker_face drowsiness 2
57293-tracker-_tracker_face noface 3

我想根据几个 if 条件更新 de type 列,但我在第 29 行收到错误:

mysql.connector.errors.InternalError: Unread result found

我很确定这是我进行查询的方式,并且在打开这个问题之前我已经搜索了与此相关的另一个问题,但无论如何我都无法解决它。

编辑1:更改了查询并应用了gzc提供的解决方案来消除错误,但现在它更新了所有类型列,而不仅仅是 if 为 true 的情况

import mysql.connector
from mysql.connector import errorcode
import os

cnx = mysql.connector.connect(user='root', database='empresa')
cursor = cnx.cursor()


fileDir = os.path.dirname(os.path.realpath(__file__))
textDir = os.path.join(fileDir, "test_img")


query = ("SELECT nome_imagem, estado, type FROM alertas ")

cursor.execute(query)
results = list(cursor)

for (nome_imagem, estado, type) in results:
print nome_imagem, estado

my_file_name = nome_imagem+'.txt'
my_file = open("test_img/"+my_file_name, 'r')
content = my_file.readline()
status = content.strip().split()[-1].split("=")[1]
face = content.strip().split()[0].split("=")[1]
print status, face #1 tem face, 0 nao tem

if (face == '1' and estado == status): #se tem cara e o estado que tem na bd for igual ao estado que o programa classificou = correto
print "correto"
cursor.execute("UPDATE alertas SET type='1' WHERE nome_imagem=nome_imagem")
if (face == '1' and estado == 'drowsiness' and status == 'awake') or (face == 1 and estado == 'awake' and status == 'drowsiness'): #verificar isto
print "trocado"
if (estado != '' and face == '0'): # se tiver estado mas nao tiver cara classifico logo como errado 3
print "errado"
if (estado == 'distracted' and face == '1'): # se tem cara mas for distracted deixo normal pois nao consigo classificar
print "normal"



cursor.close()
cnx.close()

我做错了什么?

谢谢

编辑 2: gzc 再次求解:)

最佳答案

在对同一光标执行其他操作之前,您必须读取所有结果。

...
query = ("SELECT nome_imagem, estado, type FROM alertas ")

cursor.execute(query)

results = list(cursor)

for (nome_imagem, estado, type) in results:
...

关于python - 使用Python更新mysql错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41702248/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com