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

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

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

你知道我哪里做错了吗?


当前回答

如果您正在寻找DRUPAL环境

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

drush sqlc

其他回答

很可能,你只需要改变斜杠/黑斜杠: 从

 \home\sivakumar\Desktop\test.sql

to

 /home/sivakumar/Desktop/test.sql

因此,命令将是:

source /home/sivakumar/Desktop/test.sql

你可以使用下面的命令来执行已经写入文本文件的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

给出.sql文件的路径:

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

而不是重定向,我会做以下事情

mysql -h <hostname> -u <username> --password=<password> -D <database> -e 'source <path-to-sql-file>'

这将执行文件path-to-sql-file

你有很多选择:

MySQL命令行客户端:MySQL -h hostname -u user database < path/to/test.sql 安装MySQL GUI工具并打开SQL文件,然后执行它 如果数据库可以通过web服务器访问,请使用phpmysql