为什么我无法从命令行 运行 我的 SQL 文件?
Why am I not able to run my SQL file from command line?
我可以使用 cmd
中的以下过程手动将 MySQL 模式从 .sql
转储文件导入我的数据库:
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
我一直在尝试使用 subprocess.run
使用以下代码自动执行此操作:
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)
但是,我收到以下错误:
ERROR 1059 (42000): Identifier name ' \. c:\users\philip\onedrive\betting\capra\tennis\polgara\tests\mysql\schema_belgarath_test_create.s' is too long
为了解决这个问题,我尝试使用 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)
但是,我现在收到以下错误:
ERROR 1049 (42000): Unknown database ' \. schema_belgarath_test_create.sql'
在传递命令\. file.sql
之前需要使用-e
,并且命令需要使用引号。
例如:
mysql.exe -uroot -proot -e "\. file.sql"
因此,您的 python 代码应该是:
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)
我可以使用 cmd
中的以下过程手动将 MySQL 模式从 .sql
转储文件导入我的数据库:
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
我一直在尝试使用 subprocess.run
使用以下代码自动执行此操作:
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)
但是,我收到以下错误:
ERROR 1059 (42000): Identifier name ' \. c:\users\philip\onedrive\betting\capra\tennis\polgara\tests\mysql\schema_belgarath_test_create.s' is too long
为了解决这个问题,我尝试使用 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)
但是,我现在收到以下错误:
ERROR 1049 (42000): Unknown database ' \. schema_belgarath_test_create.sql'
在传递命令\. file.sql
之前需要使用-e
,并且命令需要使用引号。
例如:
mysql.exe -uroot -proot -e "\. file.sql"
因此,您的 python 代码应该是:
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)