简体   繁体   English

java - mysql - select query outfile - 保存文件的位置

[英]java - mysql - select query outfile - where is file getting saved

connecting to a mysql database from java using jdbc. 使用jdbc从java连接到mysql数据库。 declaring a query 声明查询

String query = 
                    "SELECT *"+
                    "FROM tt2"+
                    "INTO OUTFILE 'DataFormatted.csv'"+
                    "FIELDS TERMINATED BY ','"+
                    "ENCLOSED BY '\"'" +
                    "LINES TERMINATED BY '\n'";

executing query using executQuery(query). 使用executQuery(查询)执行查询。

how to change above code to save DataFormatted.csv into c drive root directory 如何更改上面的代码以将DataFormatted.csv保存到c盘根目录

where is file getting saved. 文件保存在哪里。

In the current working directory of the MySQL server. 在MySQL服务器的当前工作目录中。 Which one it is depends on how the MySQL server is executed and configured. 它取决于MySQL服务器的执行和配置方式。 Best is to change the location of the CSV file to a fixed location. 最好将CSV文件的位置更改为固定位置。

how to change above code to save DataFormatted.csv into c drive root directory 如何更改上面的代码以将DataFormatted.csv保存到c盘根目录

Just change 'DataFormatted.csv' by 'C:/DataFormatted.csv' . 只需将'DataFormatted.csv'改为'DataFormatted.csv' 'C:/DataFormatted.csv'

Be aware that both Java code and the MySQL server should run at physically the same machine if you want to access the CSV file by Java as well. 请注意,如果您还想通过Java访问CSV文件,Java代码和MySQL服务器应该在物理上在同一台机器上运行。 If they runs at physically different machines, then you'll probably look for other ways to access the CSV file, eg FTP'ing the generated CSV file. 如果它们在物理上不同的机器上运行,那么您可能会寻找其他方式来访问CSV文件,例如FTP“生成的CSV文件”。

Assuming your SQL query is correct (your example is missing spaces at line break), the OUTFILE saves the File on the server. 假设您的SQL查询是正确的(您的示例在换行时缺少空格),OUTFILE将文件保存在服务器上。
If no path is given, it will go in the Data Directory, in a subdirectory with the user's name. 如果没有给出路径,它将进入数据目录,在具有用户名称的子目录中。
It's something like the C:\\Program Files\\MySQLServer\\data\\test for the test user. 它类似于test用户的C:\\Program Files\\MySQLServer\\data\\test

To find out what the data directory is, use the show variables where variable_name = 'datadir' query. 要找出数据目录是什么,请使用show variables where variable_name = 'datadir'

To change the location of OUTFILE just use the complete path, as suggested by BalusC . 要改变OUTFILE的位置,只需使用完整路径,如BalusC所建议的那样。

SELECT ... INTO OUTFILE

dumps the data into a file on the MySQL server machine. 将数据转储到MySQL 服务器计算机上的文件中。 If you want to create a local file on the Java client, you will need to create it yourself. 如果要在Java客户端上创建本地文件,则需要自己创建。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM