Why am I not able to run my SQL file from command line?

I’m able to manually import a MySQL schema from a .sql dump file into my database using the following process from cmd:

cd C:\Users\Philip\OneDrive\Betting\Capra\Tennis\polgara\tests\mysql # Go to directory where .sql file is  "C:\Program Files\MySQL\MySQL Server 8.0\bin\mysql.exe" -uroot -proot # Run MySQL  \. schema_belgarath_test_create.sql # Run SQL 

I’ve been trying to automate this using subprocess.run using the following code:

mysql_exe = r'C:\Program Files\MySQL\MySQL Server 8.0\bin\mysql.exe' mysql_dump = r'C:\Users\Philip\OneDrive\Betting\Capra\Tennis\polgara\tests\mysql\schema_belgarath_test_create.sql' args = [mysql_exe, '-uroot', '-proot', r' \. ' + mysql_dump] subprocess.run(args) 

However, I get the following error:

ERROR 1059 (42000): Identifier name ' \. c:\users\philip\onedrive\betting\capra\tennis\polgara\tests\mysql\schema_belgarath_test_create.s' is too long 

To get around this I tried changing the directory using cwd=:

mysql_exe = r'C:\Program Files\MySQL\MySQL Server 8.0\bin\mysql.exe' mysql_dump_path = r'C:\Users\Philip\OneDrive\Betting\Capra\Tennis\polgara\tests\mysql' belgarath_sql = 'schema_belgarath_test_create.sql' args = [mysql_exe, '-uroot', '-proot', r' \. ' + belgarath_sql] subprocess.run(args, cwd=mysql_dump_path) 

However, I now get the following error:

ERROR 1049 (42000): Unknown database ' \. schema_belgarath_test_create.sql' 
Add Comment
1 Answer(s)

You need to use -e before passing the command \. file.sql, and you need to use quotation marks for the command.

For example:

mysql.exe -uroot -proot -e "\. file.sql" 

Therefore, you python code should be:

mysql_exe = r'C:\Program Files\MySQL\MySQL Server 8.0\bin\mysql.exe' mysql_dump = r'C:\Users\Philip\OneDrive\Betting\Capra\Tennis\polgara\tests\mysql\schema_belgarath_test_create.sql' mysql_cmd = r'"\. {}"'.format(mysql_dump) args = [mysql_exe, '-uroot', '-proot', '-e', mysql_cmd] subprocess.run(args) 
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.