MySQL why cursor.execute(sql, multi=True) does not work but 2 cursor.execute(sql) works?

后端 未结 1 1246
遇见更好的自我
遇见更好的自我 2021-01-22 13:41

This code works:

sql = \"\"\"TRUNCATE TABLE product_groups;\"\"\"
cursor.execute(sql)

sql = \"\"\"INSERT INTO product_groups (origin, type, name, brand, concent         


        
相关标签:
1条回答
  • 2021-01-22 14:00

    This statement:

    cursor.execute(sql, multi=True)
    

    creates an iterator over the results. It looks like it's lazy (i.e., it executes SQL statements only as needed). You're never asking for the results for the second statement, so it is only executing the first one. Try:

    for _ in cursor.execute(sql, multi=True): pass
    

    In general it's better to just use separate execute() calls.

    0 讨论(0)
提交回复
热议问题