Sunday, August 5, 2012
IMPORTING DATA FROM MULTIPLE .DSV FILES TO MULTIPLE TABLES IN MySQL db
COUNT OF FILES IN A DIRECTORY:-
root> # ls -l |grep '^-' | wc -l
--------------------------------------------------------------------
TRANSFERING ALL FILENAMES IN A DIRECTORY TO A FILE:-
root> # ls -1 > /home/meena/Desktop/meena/DSV_FILENAMES.txt
--------------------------------------------------------------------
AWK COMMAND TO CREATE STATEMENTS LIKE BELOW FOR EACH TABLE:-
LOAD DATA LOCAL INFILE /home/meena/Desktop/meena/TABLE1.dsv
INTO TABLE TABLE1
FIELDS ENCLOSED BY '"' SEPERATED BY '\t'
LINES TERMINATED BY '\n'
IGNORE 1 LINES;
root> #
awk -F: -v sq="'" -F: -v dq="\"" -F: -v t="\\t" -F: -v n="\\n"
'BEGIN{FS=" ";RS="\n";ORS=";\n\n"}
{print "LOAD DATA LOCAL INFILE " sq "/home/meena/Desktop/meena/" $1 sq " INTO TABLE " $1 " FIELDS ENCLOSED BY " sq dq sq " TERMINATED BY " sq t sq " LINES TERMINATED BY " sq n sq " IGNORE 1 LINES"}'
/home/meena/Desktop/meena/DSV_FILENAMES.txt
> /home/meena/Desktop/meena/DSV_IMPORT_STMT.sql
REPLACE ALL
_DATA.dsv FIELDS
WITH
FIELDS
root> # mysql -uroot -proot set_foreign_key_checks=0;
root> # mysql -uroot -proot db_name > /home/meena/Desktop/meena/DSV_IMPORT_STMT.sql
root> # mysql -uroot -proot set_foreign_key_checks=1;
Labels:
MySQL
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment