Fri, 17 May 2024


How to export a MySQL Table to a Flat File?

By: Anton, NetArt Media
Fri, 17 December 2021

The default format of a file exported from MySQL is as follows: the fields are separated by tabs, the lines terminated by '\n', and backslashes(\), newlines (\n), and tabs (\t) escaped by a backslash. NULL is exported as \N.
This example exports the data in a table called mysql_table2 to a file named outfile.txt.

try
{
// Create the statement
Statement stmt = connection.createStatement();

// Export the data
String filename = "c:\\\\temp\\\\outfile.txt";
String tablename = "mysql_2_table";
stmt.executeUpdate("SELECT * INTO OUTFILE \"" + filename + "\" FROM " + tablename);
}

catch (SQLException e)

{
}


Category: Web Development
Share this post:



See All Scripts






Subscribe for our newsletter

Receive the latest blog posts direct in your mailbox