I have a .sql file containing a bunch of SQL queries, with each query spanning multiple lines. I want to execute these queries in MySQL via Python using MySQLdb.
sqlite3has a "non-standard shortcut" for this purpose, calledexecutescript() , but there seems to be no equivalent function in MySQLdb.
I noticed this old question from 2 years ago that asks the same thing, but I found the answers unsatisfactory. The answers are mostly:
Use subprocessto run the command mysqland send it to the .sql file.
This works, but it is rather inelegant, and it introduces undesirable complexity in error handling, etc.
If each query is on the same line, just run each line separately.
But in my case, they span multiple lines, so this will not work.
If each request is not on the same line, somehow join them.
But how? I mean, I can hack something simple enough, so you don’t have to answer with semi-white answers here, and maybe this is what I will eventually do, but is there an already installed library that does this? I would feel more comfortable with a complete and correct solution, rather than with a hack.
source
share