MySQLdb.cursor.execute can't run multiple queries

前端 未结 7 1334
暗喜
暗喜 2020-12-06 05:11

We\'re trying to run SQL files containing multiple insert statements as a single query, but it seems rollback fails when any of the statements contain an error.

7条回答
  •  情深已故
    2020-12-06 05:57

    Tried the multi=True method, but ended up splitting the file by semi and looping through. Obviously not going to work if you have escaped semis, but seemed like the best method for me.

    with connection.cursor() as cursor:
        for statement in script.split(';'):
            if len(statement) > 0:
                 cursor.execute(statement + ';')
    

提交回复
热议问题