【问题标题】:Why the cursor is not created again?为什么没有再次创建游标?
【发布时间】:2019-08-12 02:41:39
【问题描述】:

我有这段代码,它只在第一次执行循环时返回数据。其他时候,尽管数据库中有数据并且查询定义明确,但游标不返回任何内容。而且不知道为什么,因为每次执行循环,都会创建连接和游标。

def get_team_colour_map(self, players, id_competition):
    tcm = FIBAColourMap()
    for p in players:
        args = [p["id"], id_competition]
        conn = pymysql.Connect(host = DDBB.DDBB_FIBA_HOST,
                                      user = DDBB.DDBB_FIBA_USER,
                                      password = DDBB.DDBB_FIBA_PSWD,
                                      db = DDBB.DDBB_FIBA_NAME,
                                      charset = DDBB.DDBB_FIBA_CHARSET,
                                      cursorclass=pymysql.cursors.DictCursor)
        with conn.cursor() as cursor:
            print("id player: {}".format(p["id"]))
            print("args: {}".format(args))
            cursor.execute("select sc.* from tbl030_shots_chart sc, tbl006_player_team pt, tbl007_game g, tbl004_jornada j, tbl012_competition c where pt.id = %s and pt.id_player_feb = sc.id_fiba and sc.id_game = g.id and g.id_jornada = j.id and j.id_competition = c.id and c.id = %s", args)
            data = cursor.fetchall()
            print("data: {}".format(data))
            print("Total rows: {}".format(cursor.rowcount))
            if cursor.rowcount > 0:
                for s in data:
                    x = float(FIBASCReport.adjust_x(s["x"]))
                    y = float(FIBASCReport.adjust_y(s["y"]))
                    color = tcm.image.getpixel((x,y))
                    color = ("#%02x%02x%02x" % color).upper()
                    if tcm.exists_color(color):
                        if int(s["m"]) == 0:
                            tcm.set_scored_shots(color, 1)
                        else:
                            tcm.set_failed_shots(color, 1)
                    else:
                        if int(s["m"]) == 0:
                            tcm.set_scored_shots("OTROS", 1)
                        else:
                            tcm.set_failed_shots("OTROS", 1)
            else:
                #tcm = None
                print("Jugadora con id: {} NO ha realizado ningún tiro en competición: {}".format(p["id"], id_competition))
    return tcm

在此代码中,cursor.fetchall() 在第一个查询中返回数据,但下一个查询返回空结果。

【问题讨论】:

    标签: python mysql python-3.x pymysql


    【解决方案1】:

    尝试在循环运行后重置光标,以便在每个循环上获得一个“干净”的光标:

     if cursor.rowcount > 0:
                for s in data:
                    x = float(FIBASCReport.adjust_x(s["x"]))
                    y = float(FIBASCReport.adjust_y(s["y"]))
                    color = tcm.image.getpixel((x,y))
                    color = ("#%02x%02x%02x" % color).upper()
                    if tcm.exists_color(color):
                        if int(s["m"]) == 0:
                            tcm.set_scored_shots(color, 1)
                        else:
                            tcm.set_failed_shots(color, 1)
                    else:
                        if int(s["m"]) == 0:
                            tcm.set_scored_shots("OTROS", 1)
                        else:
                            tcm.set_failed_shots("OTROS", 1)
                    cursor.reset()
    

    【讨论】:

    • 嗨@Tjofoed!!!它对我不起作用:(我有一个错误:AttributeError:'DictCursor'对象没有属性'reset'
    • 啊,当然不是!我找到了这个链接,这可能会更清楚地解释一切stackoverflow.com/questions/2796517/…祝你好运!
    • 谢谢@Tjofed,但我的问题是另一个问题。我不知道为什么在每个 for 循环中重新启动连接时没有再次创建游标。我不想在查询后来回前进。我想在每次执行 for 循环时进行查询。每次启动 for 循环时,都会再次创建连接。而且我不知道为什么它第一次工作,但是当我在 for 循环启动的所有时间创建连接时其他时间都不起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-15
    • 2018-09-04
    • 2011-01-03
    • 1970-01-01
    • 2014-09-09
    相关资源
    最近更新 更多