Along with the script in the project folder is the .sql database file, can I work with this file in my project? If so, how?
1 answer
The pymysql module pymysql not provide the ability to execute the entire SQL file.
pymysql allows pymysql to execute individual SQL commands.
BUT breaking a SQL file into SQL commands is a very nontrivial task. The file may contain multiline commands, comments, explicit transactions, procedures, functions, triggers, etc. In general, you will have to repeat all the work that the creators of the MySQL client / MySQL Shell done. It will be more logical to use the results of their work - start the mysql client from Python :
import subprocess cmd = ['/path/to/mysql', db, '-u', user, '-p', passwd, '<', '/path/to/script.sql'] output = subprocess.check_output(cmd) PS The mysql binary should be located on the same machine as the Python script.
|