MySQLdb.cursor.execute can't run multiple queries

一世执手 提交于 2019-11-28 11:58:58

Like all Python DB-API 2.0 implementations, the cursor.execute() method is designed take only one statement, because it makes guarantees about the state of the cursor afterward.

Use the cursor.executemany() method instead. Do note that, as per the DB-API 2.0 specification:

Use of this method for an operation which produces one or more result sets constitutes undefined behavior, and the implementation is permitted (but not required) to raise an exception when it detects that a result set has been created by an invocation of the operation.

Using this for multiple INSERT statements should be just fine:

cursor.executemany('INSERT INTO table_name VALUES (%s)',
    [(1,), ("non-integer value",)]
)

If you need to execute a series of disparate statements like from a script, then for most cases you can just split the statements on ; and feed each statement to cursor.execute() separately.

I think you need to pass multi=True to execute when using multiple statements, see http://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursor-execute.html

Update: This applies to the mysql.connector module, not MySQLdb used in this case.

Apparently there is no way to do this in MySQLdb (aka. MySQL-python), so we ended up just communicateing the data to subprocess.Popen([mysql, ...], stdin=subprocess.PIPE) and checking the returncode.

DavidSM

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 + ';')

Using the mysql program via Popen will definitely work, but if you want to just use an existing connection (and cursor), the sqlparse package has a split function that will split into statements. I'm not sure what the compatiblity is like, but I have a script that does:

with open('file.sql', 'rb') as f:
    for statement in sqlparse.split(f.read()):
        if not statement:
            continue
        cur.execute(statement)

It's only ever fed DROP TABLE and CREATE TABLE statements, but works for me.

https://pypi.python.org/pypi/sqlparse

use below line item to execute statement :

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!