If you have access to the mysql
client, then this is trivial:
mysql -u username -p database < input.sql
This will prompt you for a password; if you want to supply the password inline:
mysql -u username -ppassword database < input.sql
Run any of the above from Python using the subprocess
module.
The not-so-easy way would be to read each line of the file - do some logic to grab entire SQL statements - and then execute it against the database "manually", but this would just replicate what the mysql
command is doing:
import MySQLdb
db = MySQLdb.connect(username="foo",passwd="secret",db="mydb")
c = db.cursor()
with open('input.sql') as f:
for line in f:
proper_sql = some_magic_function(line)
c.execute(proper_sql)
db.commit()