【问题标题】:How to execute more than once the same query with different data?如何使用不同的数据多次执行相同的查询?
【发布时间】:2019-03-21 02:40:47
【问题描述】:

我正在尝试使用不同的数据执行相同的查询,但我总是第一次获得数据。其他时候,尽管数据库中有查询的数据,mysql返回空数据。

这是代码:

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() 在第一个查询中返回数据,但下一个查询返回空结果。

如何运行多个查询?我正在使用 mySQL 8.0 和 Python 3.6

【问题讨论】:

标签: python mysql python-3.x pymysql


【解决方案1】:

这是因为您每次都使用相同的光标。每次循环执行查询时创建一个新的游标实例。运行第一个查询后,游标已经定位在所有数据之后。因此之后没有返回任何行

你也可以试试这个:

查看 MySQLCursor.execute() 的文档。

它声称您可以传入一个 multi 参数,该参数允许您在一个字符串中运行多个查询。

如果multi设置为True,execute()可以执行操作字符串中指定的多条语句。

multi 是 execute() 调用的可选第二个参数:

operation = 'SELECT 1; INSERT INTO t1 VALUES (); SELECT 2'
for result in cursor.execute(operation, multi=True):

【讨论】:

  • 但是,我怎样才能创建一个新的光标呢?我认为每次循环完成时我都会创建它。如果我在获取数据后关闭光标,我仍然会收到同样的错误:(
  • 尝试修改您的代码,以便您可以一次运行多个语句。然后创建一个字符串,其中包含您要运行的所有查询,用逗号分隔。操作 = '选择 1;插入 t1 值 (); SELECT 2' 然后像这样遍历它们以获得 cursor.execute(operation, multi=True):
  • 感谢您的帮助。我想您的解决方案很好,但我不明白为什么每次执行循环时都没有再次创建游标,因为再次创建了连接。我什么都不懂:(((((((
  • 如果我的解决方案有效/帮助了你。请标记为答案:)
猜你喜欢
  • 2020-12-01
  • 1970-01-01
  • 2012-03-23
  • 2013-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-19
相关资源
最近更新 更多