How to load data as text file in mysql? - mysql

How can I load data as text file in "file" column in mysql table?
If my table is: id,name,file
I don't want to use text file to load data in my table, I want to store the actual .txt files in my table.

LOAD DATA INFILE 'file_name.txt'
INTO TABLE database.table_name
FIELDS TERMINATED BY '\t'
ENCLOSED BY ''
ESCAPED BY '\\'
LINES TERMINATED BY '\n'
STARTING BY ''

LOAD DATA INFILE 'data.txt' INTO TABLE db2.my_table;
LOAD DATA LOCAL INFILE '/tmp/foo.txt'
INTO TABLE foo COLUMNS TERMINATED BY '\t';

Related

How to insert selected columns from a CSV file into one MySQL field using LOAD DATA INFILE [duplicate]

I have a file with some MySQL commands for loading a single column into a database table:
TRUNCATE TABLE datamap;
LOAD DATA LOCAL INFILE 'datamap.txt'
INTO TABLE datamap
Fields terminated by ','
enclosed by '"'
escaped by '^'
Lines terminated by '\r\n'
(datapath);
The datapath file contains a list of files and their directories, i.e. x:\files\filename1.txt
All files are now in a single directory and I would like to hard-code the path into the command above, such that I can load only the filename but have the full path saved to the column in my table. I am aware that I could add an additional UPDATE statement above to update every column in the table after I've loaded it but that doesn't seem very efficient.
Does the LOAD DATA INFILE command support concatenating fixed strings with data being read in from a file?
TRUNCATE TABLE datamap;
LOAD DATA LOCAL INFILE 'datamap.txt'
INTO TABLE datamap
Fields terminated by ','
enclosed by '"'
escaped by '^'
Lines terminated by '\r\n'
(col1, col2, #my_variable)
SET col3 = CONCAT('x:\files\', #my_variable);
http://dev.mysql.com/doc/refman/5.6/en/load-data.html
LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE 'file_name'
...
[(col_name_or_user_var,...)]
[ SET col_name = expr,...]
When processing an input line, LOAD DATA splits it into fields and uses the values according to the column/variable list and the SET clause, if they are present. Then the resulting row is inserted into the table.

load csv file in mysql table using LOAD DATA LOCAL INFILE command

I want to load csv file into mysql table.The query works fine but in csv row is like :
1000002,Kabul,"Kabul,Afghanistan",2004,AF,City,Active
Kabul and Afghanistan goes into 2 separate columns.below is my query:
LOAD DATA LOCAL INFILE "'.$file.'"
INTO TABLE '.$table.'
FIELDS TERMINATED by \',\'
LINES TERMINATED BY \'\n\'
I want "Kabul,Afghanistan" in one column.
The problem is that you need to add
OPTIONALLY ENCLOSED BY '"'
so that the loader knows to treat the quoted string as a single field.

mysql load data infile it contain more data than there were input column

I'm a new to mysql, I try load csv file to mysql.
the csv like:
1,"a,b"
2,bc
3,d
the table like this:
create table test(ind varchar(10),var varchar(20));
when I load this csv file:
load data infile 'test.csv' into table test
fields terminated by ',' ;
I change this
the warning:
row 1 was truncated: it contained more data than there were input columns
I try this:
load data infile 'test.csv' into table test
fields terminated by ','
optionally enclosed by '"'
it doesn't work.
the common of "a,b" cause this error. but I don't know how to solve this question.
It sounds like maybe LOAD DATA isn't properly picking up on your line breaks. Try adding LINES TERMINATED BY ... to your call:
LOAD DATA INFILE 'test.csv' INTO TABLE test
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\r\n' -- use '\n' if on Linux
(ind, var)
With the above call, MySQL should not view the comma inside the first quoted term "a,b" as being a field separator, but rather just part of the text of that column.

Can I concatenate a hard-coded string with a column in a MySQL LOAD DATA INFILE?

I have a file with some MySQL commands for loading a single column into a database table:
TRUNCATE TABLE datamap;
LOAD DATA LOCAL INFILE 'datamap.txt'
INTO TABLE datamap
Fields terminated by ','
enclosed by '"'
escaped by '^'
Lines terminated by '\r\n'
(datapath);
The datapath file contains a list of files and their directories, i.e. x:\files\filename1.txt
All files are now in a single directory and I would like to hard-code the path into the command above, such that I can load only the filename but have the full path saved to the column in my table. I am aware that I could add an additional UPDATE statement above to update every column in the table after I've loaded it but that doesn't seem very efficient.
Does the LOAD DATA INFILE command support concatenating fixed strings with data being read in from a file?
TRUNCATE TABLE datamap;
LOAD DATA LOCAL INFILE 'datamap.txt'
INTO TABLE datamap
Fields terminated by ','
enclosed by '"'
escaped by '^'
Lines terminated by '\r\n'
(col1, col2, #my_variable)
SET col3 = CONCAT('x:\files\', #my_variable);
http://dev.mysql.com/doc/refman/5.6/en/load-data.html
LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE 'file_name'
...
[(col_name_or_user_var,...)]
[ SET col_name = expr,...]
When processing an input line, LOAD DATA splits it into fields and uses the values according to the column/variable list and the SET clause, if they are present. Then the resulting row is inserted into the table.

How to import csv to mysql table?

Error: This query is just inserting the 1 line of file
LOAD DATA INFILE 'file.csv' INTO TABLE coords FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n';
And the file has 300k lines.
Columns are separeted by commas.
How is the coords created? Does it have 10 attributes for the data?
I just ran a quick test with a file containing six identical lines (using your data) and entered:
LOAD DATA LOCAL INFILE 'file.csv' INTO TABLE coord FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n';
It read all 6 lines in.
I did this under Linux. If you're on Windows maybe you need '\r\n' (?).