How to import CSV into mysql if values contains comma - mysql

I've a CSV file where values may have comma as part of value
"09200, France, Paris", "Tower ""Olivia"""
"09200, Spain, Barselona", Shop - perfect
However once I import data it breaks value on 4 columns (based on number of comma in row). What do I do wrong? Please see my import command below.
LOAD DATA LOCAL INFILE '~/Downloads/file.csv' INTO TABLE my_table CHARACTER SET utf8 FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' (#col1,#col2) set address=#col1,name=#col2;

Add an ENCLOSED BY clause to your query:
LOAD DATA LOCAL INFILE '~/Downloads/file.csv'
INTO TABLE my_table
CHARACTER SET utf8
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
(#col1,#col2) set address=#col1,name=#col2;

Related

MySQL Load Data Query: Issue with Backslash ( \ )

I have I have a huge csv file with 149 column and 25K+ rows to upload this file in MySQL table I am using MySQL LOAD DATA Query
MY Query is:
LOAD DATA local INFILE '/Dir/file.csv' INTO TABLE my_table FIELDS TERMINATED BY ',' ENCLOSED BY '"' ESCAPED BY '\\' LINES TERMINATED BY '\n' IGNORE 1 LINES
My query is working fine, the problem comes when my file has any Backslash (\) characters, the column value get disturb and file cell value not inserting in correct columns. is there any fix for this issue.
Thanks
LOAD DATA local INFILE '/Dir/file.csv' INTO TABLE my_table FIELDS TERMINATED BY ',' ENCLOSED BY '"' ESCAPED BY '\b' LINES TERMINATED BY '\n' IGNORE 1 LINES
Use '\b' as escape character instead of '\' in your query.

loading Semicolon separated values to mysql

I've values in a csv file like below
2017-05-01 00:00:04.651;Mother Telecommunication;Unique Infoway ltd;GP;01759257722;00966570053415;SAUDI ARABIA;Saudi Arabia-Al Jawwal GSM Network-mobile;9665;0.02;15;1;4;3.06;0.94;0.141;3.201
at a row.
I want to load it to mysql using
LOAD DATA INFILE '/var/www/html/processed_mysql_csv/xyz_details_report_20170810190128.csv'
INTO TABLE GP_UIL_BILL_RECON
FIELDS TERMINATED BY ';'
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
but all the values stored only in first column.
What can I do to place values to their related columns

How do I replace a character on a specific column of data when doing a CSV import into MySQL?

Here's what I am using to import my CSV file:
LOAD DATA LOCAL INFILE '/Users/username/Desktop/sample-mac.csv'
INTO TABLE stamps
CHARACTER SET utf8
FIELDS OPTIONALLY ENCLOSED BY '"' TERMINATED BY ','
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;
What I am trying to do is get rid of all { and } characters in the qm column when importing my CSV file. How can I do that in this SQL?

Importing csv into MySQL

I've got a csv file I made with a bunch of info, but I cant get it to import properly...
Ive got these values in info.csv: id firstname lastname address state gpa credits
following a video, I used this:
LOAD DATA LOCAL INFILE '/IT101/info.csv' INTO TABLE 'student' FIELDS TERMINATED BY ','
ENCLOSED BY '"' ESCAPED BY '\' LINES TERMINATED BY '\n';
I get back:
PAGER set to stdout
and the values aren't there. What am I doing wrong?
EDIT: By the way, I alaready have two rows in the table from using insert into, just doing it 30 more times seemed like a waste of time
You can try...
LOAD DATA INFILE '/IT101/info.csv'
INTO TABLE students
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;

Having trouble with MySQL Command 'LOAD DATA INFILE". I need to set first row as headers

Here is my code.
TRUNCATE TABLE dsw_data.inventory_sss2;
SET AUTOCOMMIT=0;
LOAD DATA INFILE 'c:/inetpub/wwwroot/Data/inventory_sss.csv' INTO TABLE dsw_data.inventory_sss2 FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\r';
COMMIT;
Here is the resulting error
Error Code: 1366. Incorrect integer value: 'CUST NO' for column 'sno' at row 1"
I want the first row of the CSV to be the table headers, and the other rows to be the data.
Also how do I specify which rows should be numeric (If this is even needed, I'm not sure).
I have searched here and google, but haven't seen code samples to set the headers like I want.
To answer your first question, you need to use the IGNORE 1 LINES syntax:
TRUNCATE TABLE dsw_data.inventory_sss2;
SET AUTOCOMMIT=0;
LOAD DATA INFILE 'c:/inetpub/wwwroot/Data/inventory_sss.csv'
INTO TABLE dsw_data.inventory_sss2
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\r';
IGNORE 1 LINES
COMMIT;
Updated to explicitly name the columns and set the primary key to null to use auto-increment:
TRUNCATE TABLE dsw_data.inventory_sss2;
SET AUTOCOMMIT=0;
LOAD DATA INFILE 'c:/inetpub/wwwroot/Data/inventory_sss.csv'
INTO TABLE dsw_data.inventory_sss2
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\r';
IGNORE 1 LINES
(your_first_column, your_second_column,...)
SET sno = NULL
COMMIT;
Add
IGNORE 1 LINES
to your LOAD DATA query as such:
LOAD DATA INFILE 'c:/inetpub/wwwroot/Data/inventory_sss.csv' INTO TABLE dsw_data.inventory_sss2 FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\r' IGNORE 1 LINES;
You do not need to specify which fields are numeric, it will based on the table you are loading into.