How to load data from csv to mysql - csv

So the structure of the csv file is like this:
Country,City,AccentCity,Region,Population,Latitude,
ad,aixas,Aixàs,06,,42.4833333,1.4666667
ad,aixirivali,Aixirivali,06,,42.4666667,1.5
ad,aixirivall,Aixirivall,06,,42.4666667,1.5
ad,aixirvall,Aixirvall,06,,42.4666667,1.5
The problem is that I don't know how to build the Load data query, I mean which separators to use and so on, mabe you guys can help me with an working example.

Does that help you?
load data local infile 'myfile.csv' into table myTable fields terminated by ','
lines terminated by '\n'
(Country,City,AccentCity,Region,Population,Latitude)

Related

Can I LOAD DATA with ugly data that has escape characters and quotes?

I have two records for example like this
11,avec myName à EX,Ex,0,2021-06-25
22,"andone \"ttt\"",Ex,0,2021-06-25
I am trying to load this into a table with MySQL with load data, and every time I try it, the quotes get cut off, or the back spaces don't show up.
I need to know if this is even possible. Can those two records go into a table and look exactly like they are in the CSV file?
I am using MySQL and trying
LOAD DATA LOCAL INFILE 'example.csv' INTO TABLE example;
Use the ENCLOSED BY and ESCAPED BY options.
LOAD DATA LOCAL INFILE 'example.csv'
INTO TABLE example
FIELDS OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '\\';
LOAD DATA LOCAL INFILE 'example.csv' INTO TABLE example
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '\\'
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES
(col1, col2, col3.col4,col5);
In mysql 8 this will get you an error please read
https://dev.mysql.com/doc/refman/8.0/en/load-data.html#load-data-file-location
Windws uses LINES TERMINATED BY '\r\n'
if the file comes from a linux system use `LINES TERMINATED BY '\n'
it os hard to tell without seeing the file what parameters would help exactly so you should try some variants out`.
also a editor with hexfile capability helps in such cases to analyse the struicture

LOAD DATA LOCAL INFILE is not working properly

this is my csv file
player_id,match_id,player,champion_id,ss1,ss2,position
9,10,1,19,Flash,Smite,JUNGLE
10,10,2,267,Exhaust,Flash,DUO_SUPPORT
11,10,3,119,Heal,Flash,DUO_CARRY
12,10,4,114,Teleport,Flash,TOP
13,10,5,112,Flash,Exhaust,MID
14,10,6,72,Smite,Flash,JUNGLE
15,10,7,3,Flash,Teleport,TOP
I use:
LOAD DATA LOCAL INFILE '/home/downloads/participants.csv'
INTO TABLE participant
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\r\n'
but the data are not loaded properly for example
the answer supposed to be 'JUNGLE' but instead only UNGLE
My theory is that this is a character encoding issue. This worked for me when I copy and pasted it into a new CSV.
It looks like it may have actually saved |ungle; not ungle.
To test this their, maybe open the CSV, delete the J and retype it manually.

Import Big csv file into mySql

I am trying to import 300 mg csv file into mySql table. I am using this command:
LOAD DATA INFILE 'c:/csv/bigCSV.csv' IGNORE
INTO TABLE table
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES
And it works great with small files (1 mg and so on), but when I try to load a big file like the one mentioned, MySql Workbench (which I use to execute my queries) runs the command, everything ok and green but 0 rows affected. No changes at all in the table.
I am 10000% sure that the table is ok because when I take a portion of that file, eg 1mg and load it into the same table it works fine.
Did anyone had this kind of problems?
Thank you.
I have "solved" it. Don`t know why and I feel stupid for not playing with the statement earlier but like this:
LOAD DATA INFILE 'c:/csv/eventsbig.csv' IGNORE
INTO TABLE std9
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
Without the "IGNORE 1 LINES" at the end and it works with files of any size.
LOAD DATA LOW_PRIORITY LOCAL INFILE 'C:\\Learning\\App6_DBconnection\\CC.csv'
INTO TABLE `test`.`ccd`
CHARACTER SET armscii8
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '"' LINES TERMINATED BY '\r\n'
IGNORE 1 LINES (`Cd_Type`, `Full_Name`, `Billing_Date`);
This will work even for large data sets of more than 1.5 million records.

Codeigniter 3, Load data infile, fcpath

Im doing Load Data Infile I've got this code
move_uploaded_file($file_temp,"uploads/master_listing/".$file_name);
$file_path = FCPATH.'/uploads/master_listing/'.$file_name;
$this->db->query("LOAD DATA LOCAL INFILE '".$file_path."'
INTO TABLE fsi_temp_newmasterlist
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\\n'
IGNORE 1 LINES
(SEQUENCENUMBER,NAME,ADDRESS,PICKUPDATE,ENCODERNO,BARCODEVALUE,REMARKS,CLIENT,PROD)
");
I dont know what's wrong in that code I got error Can't find file
Here's my file location myfolder/uploads/master_listing/
My csv file has table head of SEQUENCENUMBER,NAME,ADDRESS,PICKUPDATE,ENCODERNO,BARCODEVALUE,REMARKS,CLIENT,PROD
I really need help.. thanks in advance.
use this
$new_name = str_replace("\\","/",$file_path)
after replace
C:/xampp5.5/htdocs/joel_fsi/fsi2/uploads/master_listing/testfileupload.csv
In your code
C:\xampp5.5\htdocs\joel_fsi\fsi2/uploads/master_listing/testfileupload.csv
They are conflict \ and /
Take look at this Codeigniter path functions definitions

Load Data Local Infile SQL

I have an extract of data that is separated by using this character |
I need to load this data into a MySQL database table but I'm struggling with the lines terminated by function.
Can anyone please help me identify the correct method to terminate these fields? My example data is below.
I've already tried '"|"' but this doesn't work for the first and last fields.
Example:
00000000|OLD TRUCK|9|13|02|Z |Z |9999|111|99|ZZ|ZZ|ZZ|ZZ||ZZ|ZZ|99|999|2
Try something like this:
LOAD DATA LOCAL INFILE 'example.txt' INTO TABLE example_table
FIELDS TERMINATED BY '|'
LINES TERMINATED BY ',';
This worked for me
LOAD DATA LOCAL INFILE 'd:/test.csv' INTO TABLE test
FIELDS TERMINATED BY '|'
LINES TERMINATED BY '\n';