我想在MySQL中执行一个包含SQL查询的文本文件。
我试着运行source /Desktop/test。SQL和收到的错误:
mysql >。回家\ \ sivakumar桌面\ \测试。sql ERROR:无法打开文件 ' \ \ sivakumar \电脑\测试。Sql ',错误:2
你知道我哪里做错了吗?
我想在MySQL中执行一个包含SQL查询的文本文件。
我试着运行source /Desktop/test。SQL和收到的错误:
mysql >。回家\ \ sivakumar桌面\ \测试。sql ERROR:无法打开文件 ' \ \ sivakumar \电脑\测试。Sql ',错误:2
你知道我哪里做错了吗?
当前回答
很可能,你只需要改变斜杠/黑斜杠: 从
\home\sivakumar\Desktop\test.sql
to
/home/sivakumar/Desktop/test.sql
因此,命令将是:
source /home/sivakumar/Desktop/test.sql
其他回答
所有排在前面的答案都是好的。但如果有人想从远程服务器上的文本文件中运行查询并将结果保存到一个文件中(而不是在控制台上显示),你可以这样做:
mysql -u yourusername -p yourpassword yourdatabase < query_file > results_file
希望这能帮助到一些人。
如果你正在尝试这个命令:
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> source C:\Users\admin\Desktop\fn_Split.sql
不要指定单引号。
如果上述命令不起作用,请将文件复制到c: drive,然后重试。 如下所示:
mysql> source C:\fn_Split.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