This might be a silly question. Is there a way to execute multiple queries in one execute statement?
cursor = conn.cursor()
cursor.execute("Select * from my_tables; show tables;")
result = cursor.fetchall()
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.
cursor.execute(sql, [params])It takes one sql statement and params list (optional). Maybe you are looking for subqueries (MySQL Subqueries)?rawSQL statements like this: docs.djangoproject.com/en/dev/topics/db/sql