How can execute sql script stored in *.sql file using MySQLdb python driver. I was trying
cursor.execute(file(PATH_TO_FILE).read())
but this doesn't work because cursor.execute can run only one sql command at once. My sql script contains several sql statements instead. Also I was trying
cursor.execute('source %s'%PATH_TO_FILE)
but also with no success.
Next, db object is used to create a cursor object, which in turn is used to execute SQL queries.
From python, I start a mysql process to execute the file for me:
from subprocess import Popen, PIPE process = Popen(['mysql', db, '-u', user, '-p', passwd], stdout=PIPE, stdin=PIPE) output = process.communicate('source ' + filename)[0]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With