[Answer]-Executing django query in same statement

1👍

Looks like the CLIENT_MULTI_STATEMENTS and CLIENT_MULTI_RESULTS options are enabled by default in MySQLdb (which is quite disturbing), so you can do something like this…

>>> import MySQLdb
>>> conn = MySQLdb.connect(db='test')
>>> cur = conn.cursor()
>>> cur.execute('select * from foo; show tables;')
2L
>>> cur.fetchall()
((1L,), (1L,))
>>> cur.nextset()
1
>>> cur.fetchall()
(('foo',),)

If you want to demonstrate an example which makes changes to an InnoDB table, you’ll have to commit the transaction with something like…

>>> cur.execute('select * from foo; insert into foo values (123);')
2L
>>> cur.nextset()
1
>>> conn.commit()
>>> cur.execute('select * from foo')
3L
>>> cur.fetchall()
((1L,), (1L,), (123L,))

Check out PEP249 for the meaning of all the return values.

👤Aya

Leave a comment