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

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

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

你知道我哪里做错了吗?


当前回答

你可以使用下面的命令来执行已经写入文本文件的mysql语句:

mysql -u yourusername -p yourpassword yourdatabase < text_file

如果你的数据库还没有创建,首先登录到你的mysql使用:

mysql -u yourusername -p yourpassword

然后:

mysql>CREATE DATABASE a_new_database_name

然后:

mysql -u yourusername -p yourpassword a_new_database_name < text_file

这样就行了!

更多信息请访问:http://dev.mysql.com/doc/refman/5.0/en/mysql-batch-commands.html

其他回答

mysql> source C:\Users\admin\Desktop\fn_Split.sql

不要指定单引号。

如果上述命令不起作用,请将文件复制到c: drive,然后重试。 如下所示:

mysql> source C:\fn_Split.sql

给出.sql文件的路径:

source c:/dump/SQL/file_name.sql;

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

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

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

因为mysql -u你的用户名-p你的密码你的数据库< text_file不能在远程服务器上工作(亚马逊的EC2)…

确保首先创建了数据库。

然后:

mysql --host=localhost --user=your_username --password=your_password your_database_name < pathTofilename.sql

我犯了这个错误,我尝试了所有我能得到的建议,但都无济于事。

最后,问题是我的文件夹在文件夹名称中有一个空格,在文件夹路径中出现正斜杠,一旦我找到并删除它,它就工作得很好。