我想在MySQL中执行一个包含SQL查询的文本文件。

我试着运行source /Desktop/test。SQL和收到的错误:

mysql >。回家\ \ sivakumar桌面\ \测试。sql ERROR:无法打开文件 ' \ \ sivakumar \电脑\测试。Sql ',错误:2

你知道我哪里做错了吗?


当前回答

如果你正在尝试这个命令:

mysql -u root -proot -D database < /path/to/script.sql

你可能会得到这样的错误:如果你有特殊字符,主要是' "

ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '/path/to/script.sql' at line 1

所以我建议使用这样的命令:

echo "source /path/to/script.sql" | mysql -u root -proot -D database

该命令将执行source /path/to/script。SQL一旦连接到服务器,将执行您的脚本。

其他回答

如果你在MySQL命令行MySQL >,你必须声明SQL文件作为源文件。

mysql> source \home\user\Desktop\test.sql;
mysql -uusername -ppassword database-name < file.sql

如果您正在寻找DRUPAL环境

您可以在项目目录上使用drush命令运行

drush sqlc

我使用Bash的Here字符串进行即时SQL执行:

mysql -uroot -p <<<"select date(now())"

https://www.gnu.org/software/bash/manual/html_node/Redirections.html#Here-Strings

使用下面的mysql命令提示符-

source \\home\\user\\Desktop\\test.sql;

不要使用报价。即使路径包含空格(' '),也不要使用引号。