Update database with multiple SQL Statments

后端 未结 4 1336
终归单人心
终归单人心 2021-02-06 06:48

I am using mysql connector.Python 1.0.9 downloaded from MySQL site.

I have a sample table here

DROP TABLE IF EXISTS my_table; 
CREATE TABLE my_table
(id          


        
4条回答
  •  野性不改
    2021-02-06 07:27

    Looking at the MySQL docs

    If multi is set to True, execute() is able to execute multiple statements specified in the operation string. It returns an iterator that enables processing the result of each statement. However, using parameters does not work well in this case, and it is usually a good idea to execute each statement on its own.

    so setting multi=True returns an iterator and if you just want to loop through each statement, the other solution offered works well:

    for result in cursor.execute(SQL, multi=True):
        pass
    

提交回复
热议问题