Friday, November 24, 2017

MySQL OUTFILE & INFILE

To write dump into file (by default this file will go to the /var/lib/mysql/DBNAME/test.sql). I use NULL for id column's values because it's AUTOINCREMENT and will be generated automatically (otherwise if I dump actual value duplicate values will be created):
SELECT
 NULL,time,callid,queuename,agent,event,data1,data2,data3,data4,data5
INTO OUTFILE 'test.sql'
FIELDS TERMINATED BY ','
OPTIONALLY
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM
queue_log;

Files content is something like:
cat test.sql
\N,"2014-10-28 17:45:19.924148","NONE","NONE","NONE","QUEUESTART","","","","",""

To merge this file into an existent table:
LOAD DATA INFILE 'test.sql'
INTO TABLE queue_log
FIELDS TERMINATED BY ','
OPTIONALLY
ENCLOSED BY '"'
LINES TERMINATED BY '\n';

No comments:

Post a Comment