Importing Mysql Files

 

This is a quick guide to importing Mysql flat files into a database.

Importing the data

Connecting to the database and importing from mysqldump files

$ mysql -u dbuser -pPassword -o < dbfile.sql
$

As long as the .sql file is formatted with commands, the previous line will work just fine. This allows you to simply execute the lines of code from within the file. But what if the file is a CSV file?

$ mysql -u dbuser -pPassword
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 172

mysql> load data local infile 'dbfile.txt' 
   -> into table tblproducts fields terminated by '\t'
   -> lines terminated by '\n';

This will import the data into the table organized by tabs seperated by new lines.

 

Intellect annuls Fate.
So far as a man thinks, he is free.
-- Ralph Waldo Emerson