What is the issue with "FIELDS TERMINATED BY" and "LINES TERMINATED BY"? - mysql

So I have created a mysql table called specs and I want to import into this table a csv file.
CREATE TABLE specs (
`Id` INT NOT NULL,
`Brand` VARCHAR(40) NOT NULL,
`Horsepower` INT NOT NULL,
`Range` INT NOT NULL,
PRIMARY KEY (Id)
);
The csv data looks like the following
ID\Brand\Horsepower\Range
1\Mercedes Benz\237\634
2\Audi\345\567
3\Ford\190\456
4\BMW\278\547
5\Toyota\123\364
6\Fiat\90\289
7\Daihatsu\120\450
8\Jeep\500\670
9\Seat\119\289
10\Mitsubishi\78\410
And this is how I import the data, but the table is filled with zeros. Where is the issue. I think the problem is something with FIELDS and LINES TERMINATED BY.
LOAD DATA LOCAL INFILE '/Users/anilyelin/Desktop/cars.csv'
INTO TABLE specs
FIELDS TERMINATED BY '\\'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS
The output looks like the following:
mysql> select * from specs;
+----+-------+------------+-------+
| Id | Brand | Horsepower | Range |
+----+-------+------------+-------+
| 1 | | 0 | 0 |
| 2 | | 0 | 0 |
| 3 | | 0 | 0 |
| 4 | | 0 | 0 |
| 5 | | 0 | 0 |
| 6 | | 0 | 0 |
| 7 | | 0 | 0 |
| 8 | | 0 | 0 |
| 9 | | 0 | 0 |
| 10 | | 0 | 0 |
+----+-------+------------+-------+
10 rows in set (0,00 sec)

You should try to replace your field separator \ by a pipe |
LOAD DATA LOCAL INFILE '/Users/anilyelin/Desktop/cars.csv'
INTO TABLE specs
FIELDS TERMINATED BY '|'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS

Related

Import CSV using LOAD DATA getting wrong values

I have a big csv (near 100mb) that I would like to import in a table with the following structure:
+-------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------------+------+-----+---------+----------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| cep | varchar(255) | YES | MUL | NULL | |
| site | text | YES | | NULL | |
| cidade | text | YES | | NULL | |
| uf | text | YES | | NULL | |
| cepbase | text | YES | | NULL | |
| segmentacao | text | YES | | NULL | |
| area | text | YES | | NULL | |
| cepstatus | int(1) | YES | | NULL | |
| score | int(11) | NO | | NULL | |
| fila | int(11) | NO | | NULL | |
+-------------+------------------+------+-----+---------+----------------+
I was about to write some code to import but I've found a MySQL command that does the job to me. So I've write the following:
LOAD DATA LOCAL INFILE '/Users/user/Downloads/base.csv'
INTO TABLE cep_status_new
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\r\n'
IGNORE 1 ROWS
(#id,#cep,#site,#cidade,#uf,#cepbase,#segmentacao,#area,#cepstatus,#score,#fila)
SET id=NULL, cep=#col1, site='GOD', cidade=#col6, uf=#col7, cepbase='-', segmentacao=#col9, cepstatus=#col2, area='BING', score=99999, fila=5;
To try this code, I've removed thousand lines from my CSV and let only 2 lines: header and an input example:
cep,status,gang,bang,random,mock,awesome,qwert,hero
01019000,0,00387,00388,3550308,SAO PAULO,SP,011,B2
The code runs without problem but my insert is pretty strange:
mysql> select * from cep_status_new;
+----+------+------+--------+---------+---------+-------------+------+-----------+-------+------+
| id | cep | site | cidade | uf | cepbase | segmentacao | area | cepstatus | score | fila |
+----+------+------+--------+---------+---------+-------------+------+-----------+-------+------+
| 1 | 1 | GOD | 24655 | 3554805 | - | SP | BING | 0 | 99999 | 5 |
+----+------+------+--------+---------+---------+-------------+------+-----------+-------+------+
1 row in set (0.01 sec)
Why values from CSV are not being filled correctly?
According to this specification the column list after IGNORE 1 ROWS decides how the columns of the CSV file are mapped to columns of the table. It can either list the table columns in the order of the file or it can load the file columns into variables. With the column list
(#id,#cep,#site,#cidade,#uf,#cepbase,#segmentacao,#area,#cepstatus,#score,#fila)
you are loading 11 columns of the CSV file into variables named "id", "cep", etc. In the SET statement you then need to declare how the columns of the table are constructed from the variables. With the given statement you are refering to variables #col1 etc. that are not defined anywhere and consequently have undefined values.
The corrected statement (that I sadly can't test myself right now) should be:
INTO TABLE cep_status_new
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\r\n'
IGNORE 1 ROWS
(#col1,#col2,#col3,#col4,#col5,#col6,#col7,#col8,#col9)
SET id=NULL, cep=#col1, site='GOD', cidade=#col6, uf=#col7, cepbase='-', segmentacao=#col9, cepstatus=#col2, area='BING', score=99999, fila=5;

mySQL Command Line Import CSV Gives me NULL

I am used to using PHPmyadmin to manage my mySQL databases but I am starting to use the command line a lot more. I am trying to import a CSV file into a table called source_data that looks like this...
+-----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| code | varchar(10) | YES | | NULL | |
| result | char(1) | YES | | NULL | |
| source | char(1) | YES | | NULL | |
| timestamp | varchar(30) | YES | | NULL | |
+-----------+-------------+------+-----+---------+----------------+
And my CSV file looks like this...
code,result,source,timestamp
123 ABC,,,
456 DEF,,,
789 GHI,,,
234 JKL,,,
567 MNO,,,
890 PQR,,,
I am using this command..
LOAD DATA INFILE '/home/user1/data.csv' INTO TABLE source_data FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' IGNORE 1 ROWS;
This inserts the correct number of rows but each one just says NULL, where am I going wrong?
Since the CSV file doesn't have all the table columns (it's missing the id column), you need to specify the columns that they should be written into explicitly.
LOAD DATA INFILE '/home/user1/data.csv'
INTO TABLE source_data
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
IGNORE 1 ROWS
(code, result, source, timestamp);
Well, I hope this wouldn't be a case still, You said table name is source_data and in command the name is data

LOAD DATA LOCAL INFILE doesn't load correctly

I have Term.txt file:
1 A
2 B
3 C
Tab (\t) delimited.
I have table Term:
MariaDB [TermDS]> describe Term;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(35) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
I wanted to load this file into the table:
MariaDB [TermDS]> LOAD DATA LOCAL INFILE '/home/abigail/Term.txt' INTO TABLE Term FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\n';
Query OK, 1 row affected (0.03 sec)
Records: 1 Deleted: 0 Skipped: 0 Warnings: 0
The result isn't correct, because:
MariaDB [TermDS]> select * from Term;
+----+------+
| id | name |
+----+------+
| 1 | A
2 |
+----+------+
1 row in set (0.00 sec)
The trouble is that, it doesn't issue any error or warning message. Why doesn't the load succeed?
I got the answer. I should use terminated by '\n', instead of '\r\n'.

Load data from CSV to MySQL

I am trying to insert into mysql table from a csv file using the following command in linux:
LOAD DATA LOCAL INFILE '\/home\/abc\/mapping.csv' INTO TABLE db1.ackno_rollno_mapping FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n';
csv file is like:
166 D/O-5
208 W/O-8
230
231
236 W/O-9
245 W/O-10
8604 P/O-142
8623 W/O-730
8629 W/O-731
Table structure is:
mysql> desc db1.ackno_rollno_mapping;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| ackNo | varchar(45) | NO | PRI | NULL | |
| rollNo | varchar(45) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
The records are inserted, but some parts of both ackNo and rollNo are missing.
Like:
| 58 | W/O-728
| 67 | W/O-729
| 04 | P/O-142
| 23 | W/O-730
| 29 | W/O-731
I tried changing the datatype of ackNo from varchar(45) to integer, but still the same issue...
Am i missing something??
Thanks in Advance
--------EDIT---------
my csv file is like:
13,W/O-1
14,W/O-2
20,P/O-1
60,D/O-1
61,W/O-3
62,W/O-4
I tried below query also (removing the ENCLOSED BY '"')
LOAD DATA LOCAL INFILE '\/home\/abc\/ackno_rollno_mapping.csv' INTO TABLE db1.ackno_rollno_mapping FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n';
But the issue still is same. I noticed that the first digit of ackNo is removed in the table.
mysql> select * from db1.ackno_rollno_mapping;
+-------+-----------+
| ackNo | rollNo |
+-------+-----------+
| 3 | W/O-1
| 4 | W/O-2
| 0 | P/O-1
| 0 | D/O-1
| 1 | W/O-3
| 2 | W/O-4
Please advice.
Thanks

MySql file import (LOAD DATA LOCAL INFILE)

I have a table called city:
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| country_id | mediumint(9) | NO | MUL | NULL | |
| region_id | bigint(20) | NO | MUL | NULL | |
| city | varchar(45) | NO | | NULL | |
| latitude | float(18,2) | NO | | NULL | |
| longitude | float(18,2) | NO | | NULL | |
| timezone | varchar(10) | NO | | NULL | |
| dma_id | mediumint(9) | YES | | NULL | |
| code | varchar(4) | YES | | NULL | |
+------------+--------------+------+-----+---------+----------------+
I have a simple file (just a test file) to import:
"id","country_id","region_id","city","latitude","longitude","timezone","dma_id","code"
42231,1,833,"Herat","34.333","62.2","+04:30",0,"HERA"
5976,1,835,"Kabul","34.517","69.183","+04:50",0,"KABU"
42230,1,852,"Mazar-e Sharif","36.7","67.1","+4:30",0,"MSHA"
42412,2,983,"Korce","40.6162","20.7779","+01:00",0,"KORC"
5977,2,1011,"Tirane","41.333","19.833","+01:00",0,"TIRA"
5978,3,856,"Algiers","36.763","3.051","+01:00",0,"ALGI"
5981,3,858,"Skikda","36.879","6.907","+01:00",0,"SKIK"
5980,3,861,"Oran","35.691","-0.642","+01:00",0,"ORAN"
I run this command:
LOAD DATA LOCAL INFILE 'cities_test.txt' INTO TABLE city FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES;
Output:
Query OK, 0 rows affected (0.00 sec)
Records: 0 Deleted: 0 Skipped: 0 Warnings: 0
No records are inserted and I don't know why.
Any ideas?
Thanks!
Jamie
Worked it out. Silly mistake.
Had to change this:
LINES TERMINATED BY '\r\n'
To this:
LINES TERMINATED BY '\n'
:-)
I had the same problem, but I try this, erase the first row
`("id","country_id","region_id","city,"latitude","longitude",
"timezone","dma_id","code")` in your file to import.
Now when you run the comand write like this
mysql> LOAD DATA LOCAL
INFILE 'cities_test.txt'
INTO TABLE city FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n';
And that is all.
It worked for me :D
I had same issue on mac,
Try this if you are using mac
LOAD DATA INFILE 'sqlScript1.txt' INTO TABLE USER
FIELDS TERMINATED BY ',' LINES STARTING BY '\r';
For me, what worked on a mac was
LOAD DATA LOCAL
INFILE 'cities_test.txt'
INTO TABLE city FIELDS TERMINATED BY ','
LINES TERMINATED BY '\r';
Since Macs use carriage return for its line break you must use '/r'