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

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

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

你知道我哪里做错了吗?


当前回答

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

不要指定单引号。

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

mysql> source C:\fn_Split.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

从命令行直接传递password参数永远都不是一个好习惯,它保存在~/。Bash_history文件,可以从其他应用程序访问。

用这个代替:

mysql -u user --host host --port 9999 database_name < /scripts/script.sql -p
Enter password:

有很多种方法。

From Workbench: File > Run SQL Script -- then follow prompts

From Windows Command Line:
   Option 1: mysql -u usr -p
             mysql> source file_path.sql
   Option 2: mysql -u usr -p '-e source file_path.sql'
   Option 3: mysql -u usr -p < file_path.sql
   Option 4: put multiple 'source' statements inside of file_path.sql (I do this to drop and recreate schemas/databases which requires multiple files to be run)
             mysql -u usr -p < file_path.sql

如果从命令行得到错误,请确保之前运行过

cd {!!>>mysqld.exe home directory here<<!!}
mysqld.exe --initialize 

这必须在mysqld.exe目录中运行,也就是CD。

希望这是有用的,而不是多余的。

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

 \home\sivakumar\Desktop\test.sql

to

 /home/sivakumar/Desktop/test.sql

因此,命令将是:

source /home/sivakumar/Desktop/test.sql
mysql> source C:\Users\admin\Desktop\fn_Split.sql

不要指定单引号。

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

mysql> source C:\fn_Split.sql