Mysql table price structure:
CREATE TABLE `price` (
`code` varchar(12) ,
`date` date ,
`open` decimal(8,2) ,
`high` decimal(8,2) ,
`low` decimal(8,2) ,
`close` decimal(8,2) ,
`amount` decimal(20,2) ,
`volume` decimal(16,2)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Here is the data.csv file which i want to load into tablbe price.
000046.XSHE,19940912,20.0,20.0,16.0,16.92,121262592.0,7043300
000046.XSHE,19940913,17.0,17.32,16.0,16.46,47195860.0,2810800
000046.XSHE,19940914,16.3,16.4,15.49,15.95,24762992.0,1558300
The max value for volume is 47195860.0 which is in the range of decimal(20,2).
Load it with mysql load command.
LOAD DATA local INFILE 'data.csv'
INTO TABLE finance.price
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
lines terminated by '\r\n';
An error info occurs:
Query OK, 1 row affected, 1 warning (0.09 sec)
Records: 1 Deleted: 0 Skipped: 0 Warnings: 1
Show it to get the reason.
show warnings;
+---------+------+---------------------------------------------+
| Level | Code | Message |
+---------+------+---------------------------------------------+
| Warning | 1265 | Data truncated for column 'volume' at row 1 |
+---------+------+---------------------------------------------+
1 row in set (0.00 sec)
Number 121262592.0 is so lower than decimal(20,2),why Data truncated for column 'volume' at row 1,only one row loaded.
select * from price;
+-------------+------------+-------+-------+-------+-------+--------------+------------+
| code | date | open | high | low | close | amount | volume |
+-------------+------------+-------+-------+-------+-------+--------------+------------+
| 000046.XSHE | 1994-09-12 | 20.00 | 20.00 | 16.00 | 16.92 | 121262592.00 | 7043300.00 |
+-------------+------------+-------+-------+-------+-------+--------------+------------+
1 row in set (0.00 sec)
LOAD DATA local INFILE 'data.csv'
INTO TABLE finance.price
FIELDS TERMINATED BY ','
OPTIONAL ENCLOSED BY '"'
lines terminated by '\r\n';
Related
Create sample database and table to use with.
create database `test`;
use `test`;
create table `test` (`value` float(10,2) null);
insert into test (value) values (null);
select value from test;
+-------+
| value |
+-------+
| NULL |
+-------+
1 row in set (0.00 sec)
Now i want to create a txt file ,when you load it ,null data can be loaded into the test table.
delete from test;
Here is my data file named test.txt showed in vim(:set list).
Load the file into table.
LOAD DATA LOCAL INFILE "test.txt"
INTO TABLE test
FIELDS TERMINATED BY '\t'
LINES TERMINATED BY '\n' \w;
select value from test;
+-------+
| value |
+-------+
| 0.00 |
| 0.00 |
| 0.00 |
| 0.00 |
| 0.00 |
+-------+
How to load null data for value field with file in my case?
How to create this kind of data file,the value field is null after you load it?
The runtime environment:win10+mysql-5.7.
select version();
+-----------+
| version() |
+-----------+
| 5.7.22 |
+-----------+
1 row in set (0.03 sec)
Show test.txt file in vim with set list.
\N$
\N^I$
case:without set
Load it without (value) SET value = (CASE WHEN #value IS NULL THEN NULL ELSE #value END).
LOAD DATA LOCAL INFILE "f:/test.txt"
INTO TABLE test
FIELDS TERMINATED BY '\t'
LINES TERMINATED BY '\n' \W;
OUTPUT:
Warning (Code 1265): Data truncated for column 'value' at row 1
Warning (Code 1262): Row 2 was truncated; it contained more data than there were input columns
select * from test;
+-------+
| value |
+-------+
| 0.00 |
| NULL |
+-------+
case:with set
Load it with (value) SET value = (CASE WHEN #value IS NULL THEN NULL ELSE #value END).
delete from test;
LOAD DATA LOCAL INFILE "f:/test.txt"
INTO TABLE test
FIELDS TERMINATED BY '\t'
LINES TERMINATED BY '\n'
(value)SET value = (CASE WHEN #value IS NULL THEN NULL ELSE #value END) \W;
OUTPUT:
Warning (Code 1265): Data truncated for column 'value' at row 1
Warning (Code 1262): Row 2 was truncated; it contained more data than there were input columns
select * from test;
+-------+
| value |
+-------+
| NULL |
| NULL |
+-------+
Thank #Schwern.
LOAD DATA LOCAL INFILE "f:/test.txt"
INTO TABLE test
FIELDS TERMINATED BY '\t'
LINES TERMINATED BY '\r\n' \W;
Either works fine with no warnings.
You can use \N.
The problem is all your input lines have an extra empty field. Tab terminates the field, like a comma. You've used it as if it encloses it like quotes. For example, your first line ^Inull^I is three fields. The first is empty. The second contains null. And the third is empty.
MySQL should have said something like
Query OK, 5 rows affected, 8 warnings (0.01 sec)
Records: 5 Deleted: 0 Skipped: 0 Warnings: 8
You can read those warnings with show warnings.
+---------+------+---------------------------------------------------------------------------+
| Level | Code | Message |
+---------+------+---------------------------------------------------------------------------+
| Warning | 1265 | Data truncated for column 'value' at row 1 |
| Warning | 1262 | Row 1 was truncated; it contained more data than there were input columns |
| Warning | 1265 | Data truncated for column 'value' at row 2 |
| Warning | 1262 | Row 2 was truncated; it contained more data than there were input columns |
| Warning | 1265 | Data truncated for column 'value' at row 3 |
| Warning | 1265 | Data truncated for column 'value' at row 4 |
| Warning | 1262 | Row 4 was truncated; it contained more data than there were input columns |
| Warning | 1265 | Data truncated for column 'value' at row 5 |
+---------+------+---------------------------------------------------------------------------+
All you need is this:
\N$
Note that lines terminated by is sensitive to Unix vs Windows newlines. From the docs...
If you have generated the text file on a Windows system, you might have to use LINES TERMINATED BY '\r\n' to read the file properly, because Windows programs typically use two characters as a line terminator.
This explains the warnings you are getting.
I want to import the second column of a CSV file into MySQL. Here's the CSV file:
Name,Source,Follows
John,Youtube,Y
Kat,FB,N
Jacob,Twitter,N
Here's the code I have so far:
DROP TABLE temp;
CREATE TABLE temp
(ID INT AUTO_INCREMENT primary key,
sn VARCHAR(50)
);
DESCRIBE temp;
LOAD DATA LOCAL INFILE '/Users/...temp.csv' INTO TABLE Person
FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n'
IGNORE 1 LINES
(#col2) set sn=#col2;
SELECT * FROM temp;
However, I get that there is an empty set.
Try:
File: /path/to/temp.csv:
Name,Source,Follows
John,Youtube,Y
Kat,FB,N
Jacob,Twitter,N
MySQL Command-Line:
mysql> DROP TABLE IF EXISTS `temp`;
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE TABLE IF NOT EXISTS `temp` (
-> `ID` INT AUTO_INCREMENT PRIMARY KEY,
-> `sn` VARCHAR(50)
-> );
Query OK, 0 rows affected (0.00 sec)
mysql> LOAD DATA LOCAL INFILE '/path/to/temp.csv'
-> INTO TABLE `temp`
-> FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n'
-> IGNORE 1 LINES
-> (#`null`, `sn`, #`null`);
Query OK, 3 rows affected (0.00 sec)
Records: 3 Deleted: 0 Skipped: 0 Warnings: 0
mysql> SELECT `ID`, `sn`
-> FROM `temp`;
+----+---------+
| ID | sn |
+----+---------+
| 1 | Youtube |
| 2 | FB |
| 3 | Twitter |
+----+---------+
3 rows in set (0.00 sec)
UPDATE
mysql> LOAD DATA LOCAL INFILE '/tmp/temp.csv'
-> INTO TABLE `temp`
-> FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n'
-> IGNORE 1 LINES
-> (#`null`, `sn`);
Query OK, 3 rows affected, 3 warnings (0.00 sec)
Records: 3 Deleted: 0 Skipped: 0 Warnings: 3
mysql> SHOW WARNINGS;
+---------+------+---------------------------------------------------------------------------+
| Level | Code | Message |
+---------+------+---------------------------------------------------------------------------+
| Warning | 1262 | Row 1 was truncated; it contained more data than there were input columns |
| Warning | 1262 | Row 2 was truncated; it contained more data than there were input columns |
| Warning | 1262 | Row 3 was truncated; it contained more data than there were input columns |
+---------+------+---------------------------------------------------------------------------+
3 rows in set (0.00 sec)
mysql> SELECT `ID`, `sn`
-> FROM `temp`;
+----+---------+
| ID | sn |
+----+---------+
| 1 | Youtube |
| 2 | FB |
| 3 | Twitter |
+----+---------+
3 rows in set (0.00 sec)
Working here on bulk insert, which skips 2 records. Explanation below:
My table (works fine, Auto-incrementing Job_Id):
create table avjobs ( Job_Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
Job_Name varchar(255), Job_Seq varchar(255), Job_Date varchar(255),
Start_Time time, End_Time time, Runtime time, Status varchar(255) );
Here is my csv file:
JOB1A|0029|20140506|14:01:05|15:00:01|0:59:45|FINISHED
JOB2B|0030|20140506|15:01:05|16:00:01|0:59:55|INITIATED
Here is the BULK INSERT that I am using:
LOAD DATA LOCAL INFILE '/tmp/jobs.csv' INTO TABLE avjobs FIELDS TERMINATED BY '|' LINES TERMINATED BY '\n';
Query OK, 3 rows affected, 9 warnings (0.00 sec)
Here is output for select:
mysql> select * from avjobs;
+--------+----------+----------+----------+------------+----------+----------+--------+
| Job_Id | Job_Name | Job_Seq | Job_Date | Start_Time | End_Time | Runtime | Status |
+--------+----------+----------+----------+------------+----------+----------+--------+
| 1 | 0029 | 20140506 | 14:01:05 | 15:00:01 | 00:23:55 | 00:00:00 | NULL |
| 2 | 0030 | 20140506 | 15:01:05 | 16:00:01 | 00:59:55 | 00:00:00 | NULL |
+--------+----------+----------+----------+------------+----------+----------+--------+
The Bulk insert skips, somehow, the job name as well as status.
Can you please advise what is wrong in syntax?
You must specify your columns:
LOAD DATA LOCAL INFILE '/tmp/jobs.csv'
INTO TABLE avjobs (Job_Name, Job_Seq, Job_Date, Start_Time, End_Time, Runtime, Status)
FIELDS TERMINATED BY '|' LINES TERMINATED BY '\n';
because your import file doesn't contain your Job_ID, see manual
LOAD DATA INFILE 'persondata.txt' INTO TABLE persondata; By default,
when no column list is provided at the end of the LOAD DATA INFILE
statement, input lines are expected to contain a field for each table
column. If you want to load only some of a table's columns, specify a
column list:
I want to import data from excel to a mysql database using the command line client.
This is an example of how my csv-file is built:
Name 1 | 1 | 2 | 3 |
Name 2 | 1 | 2 | 3 |
Name 3 | 1 | 2 | 3 |
I'm using the code:
LOAD DATA LOCAL INFILE 'path to file.csv'
INTO TABLE table_name
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n';
I get the "Query OK" and with this code the formatting on the table should be almost exactly as the csv-file but I get this result:
| NULL | NULL | NULL |
| NULL | NULL | NULL |
| NULL | NULL | NULL |
What is wrong?
seems u have used '|' in your csv file as delimiters instead of
comma, try the code as
`LOAD DATA LOCAL INFILE 'path to file.csv' INTO TABLE table_name FIELDS TERMINATED BY '|'LINES TERMINATED BY '\n';`
Assume the following table structure:
CREATE TABLE `table_name` (
`name` VARCHAR(20) CHARACTER SET utf8 DEFAULT NULL,
`value1` INT(11) DEFAULT NULL,
`value2` INT(11) DEFAULT NULL,
`value3` INT(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
and the file, csv-file.csv:
Name 1,1,2,3
Name 2,1,2,3
Name 3,1,2,3
when I run the statement:
mysql> LOAD DATA INFILE '/path/csv-file.csv'
-> INTO TABLE `table_name`
-> FIELDS TERMINATED BY ','
-> LINES TERMINATED BY '\n';
mysql> SELECT `name`, `value1`, `value2`, `value3`
FROM `table_name`;
get the following result:
+--------+--------+--------+--------+
| name | value1 | value2 | value3 |
+--------+--------+--------+--------+
| Name 1 | 1 | 2 | 3 |
| Name 2 | 1 | 2 | 3 |
| Name 3 | 1 | 2 | 3 |
+--------+--------+--------+--------+
3 rows in set (0.00 sec)
I made a table which name is 'test' in mysql in my PC like belows.
create table test(
telnum varchar(20) not null,
reg_date datetime not null default '0000-00-00 00:00:00',
remarks text,
primary key(telnum)
);
And I uploaded a file named 1.txt into table 'test'.
1.txt's contents are like belows :
01011112222
01022223333
01033334444
And 'load data infile' syntax are like belows :
load data infile "c:/temp/1.txt"
ignore into table test;
But there is a problem.
Every phone numbers were cut like below.
Query OK, 3 rows affected, 3 warnings (0.00 sec)
Records: 3 Deleted: 0 Skipped: 0 Warnings: 3
mysql> select * from test;
+-------------+---------------------+---------+
| telnum | reg_date | remarks |
+-------------+---------------------+---------+
|12222 | 0000-00-00 00:00:00 |
|23333 | 0000-00-00 00:00:00 |
|34444 | 0000-00-00 00:00:00 |
+-------------+---------------------+---------+
3 rows in set (0.00 sec)
Only 5 characters are remained from 11 characters.
6 characters disappeared.
And second problem is 'warnings'. Reason of warnings is like below.
mysql> show warnings;
+---------+------+---------------------------------------------------+
| Level | Code | Message |
+---------+------+---------------------------------------------------+
| Warning | 1264 | Out of range value for column 'reg_date' at row 1 |
| Warning | 1264 | Out of range value for column 'reg_date' at row 2 |
| Warning | 1264 | Out of range value for column 'reg_date' at row 3 |
+---------+------+---------------------------------------------------+
3 rows in set (0.00 sec)
I did'nt put anything into the reg_date field.
But the message says out of range value for column 'reg_date'.
What are the reasons and how can I solve these problems?
Try to change the line delimiter. On Windows it's usually \r\n rather then \n which is default if you omit LINES TERMINATED BY clause
LOAD DATA INFILE "/tmp/1.txt"
IGNORE INTO TABLE test
LINES TERMINATED BY '\n'