MySQL Error Code 1265 : Data Truncated for column - mysql

Running into Error Code: 1265 with the following code and data.
Data was exported as CSV.
The score column containing float data appears is source of issue as without this column the data loading works.
I have tried using Decimals and this works but I curious why floats do not work yet i'm working small figures.
CREATE TABLE dummy(
entry_id INT NOT NULL,
first_name VARCHAR(15) NOT NULL,
last_name VARCHAR(15) NOT NULL,
score FLOAT NOT NULL,
PRIMARY KEY (entry_id)
);
LOAD DATA INFILE "C:/ProgramData/MySQL/MySQL Server 8.0/Uploads/test_data.csv"
INTO TABLE dummy
COLUMNS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES;
Below is the sample csv file:
entry_id,first_name,last_name,score
1,Joshua,Mbweka,3.56
2,Mary,Njoroge,25.23
3,Adam,Kimanzi,64.41
4,Eve,Faluna,56.13
5,Lester,Chuma,89.21

Related

MYSQL query data is not displaying properly

hi guys im new to mysql, i have trouble with displaying my table data, not sure what i am doing wrong..
this is my query for creating table:
CREATE TABLE sales20102017(
date DATE NOT NULL,
address VARCHAR(60) NOT NULL,
postal_code VARCHAR(10) NULL,
county VARCHAR (15) NOT NULL,
price BIGINT UNSIGNED NOT NULL,
not_full_market_price ENUM('Y', 'N') NOT NULL,
vat_exclusive ENUM('Y', 'N') NOT NULL,
property_description VARCHAR (30) NOT NULL,
size_description VARCHAR(40) NULL
);
and my load data:
LOAD DATA INFILE "C:/xampp/mysql/bin/sales20102017.csv"
INTO TABLE sales20102017
FIELDS TERMINATED BY '\n'
LINES TERMINATED BY '\n'
IGNORE 1 LINES;
and my excel data:
excel data
this is the result i get when i display it on mysql
mysql result
the table is not in order and everything is just messed up
Check for fields containing commas in csv file import on Mysql.
LOAD DATA INFILE "C:/xampp/mysql/bin/sales20102017.csv"
INTO TABLE sales20102017
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES;
See more on MySQL Reference Manual.

Mysql : load data inserting 0 instead of null

Problem: above mentioned load data query inserting 0 instead of null for price field(column)
Mysql Query :
LOAD DATA LOCAL INFILE '/tmp/data.csv'
REPLACE INTO TABLE bug_repeat
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n';
file data.csv content is as below
name,\N
name,3
bug_repeat table structure:
CREATE TABLE `bug_repeat` (
`name` varchar(10) DEFAULT NULL,
`price` decimal(12,6) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Note: above query is not working on one of MYSQL server but exact same query working fine on two other MYSQL server. I don't know what going wrong. Can some please let me know what is the exact issue.( all MYSQL servers(version 5.7.22) are on Ubuntu 16.xxx OS). I am getting same problem for bigint data type as well.
show warnings result:
1265 (01000): Data truncated for column 'price' at row 1

How does one load IP addresses into Mysql Table from a CSV file?

I'm trying to find a way to load a mysql table which I call ipCompare_tbl with IP addresses from a CSV file called myIPs.csv.
The fields in ipCompare_tbl are ipStart, and ipEnd. I've also included an auto_incrementor for a primary key generator field called id.
My goal is to have ipCompare_tbl loaded with:
ipStart ipEnd id
123.10.0.0 123.0.0.255 1
130.20.0.0 130.0.0.255 2
140.30.0.0 140.0.0.255 3
I keep getting the following error:
ERROR 1366 (HY000): Incorrect integer value: '"130.20.0.0"' for column 'ipStart' at row 1
I'm running the following code for it:
DROP TABLE IF EXISTS ipCompare_tbl;
CREATE TABLE ipCompare_tbl(
ipStart int(10) unsigned NOT NULL,
ipEnd int(10) unsigned NOT NULL,
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY ( id ));
LOAD DATA INFILE 'myIPs.csv' INTO TABLE ipCompare_tbl
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY ';'
IGNORE 1 LINES;
SELECT INET_ATON(ipStart), INET_ATON(ipEnd)
FROM ipCompare_tbl;
Change your table schema to
CREATE TABLE ipCompare_tbl(
ipStart varchar(15) NOT NULL,
ipEnd varchar(15) NOT NULL,
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY ( id ));
You need to convert the IP addresses to numbers when you're loading the CSV.
LOAD DATA INFILE 'myIPs.csv' INTO TABLE ipCompare_tbl
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY ';'
IGNORE 1 LINES
(#ipstart, #ipend, id)
SET ipStart = INET_ATON(#ipstart), ipEnd = INET_ATON(#ipend);
Then you don't need to use INET_ATON when you're selecting, since it's already numbers.

MySql Import strange behaviour

I am trying to import a csv file that is delimited by tabs.
Here is my query
LOAD DATA LOCAL INFILE 'c:/news.csv'
INTO TABLE news
FIELDS TERMINATED BY '\t'
LINES TERMINATED BY '\r'
(url, storyid, title, date, details, category, author);
What happens is only the first column is loaded, (url).
The rest shows NULL. I have tried lines terminated by \n as well. Same result.
Any advice?
Table structure for table `news`
--
CREATE TABLE IF NOT EXISTS `news` (
`url` varchar(62) DEFAULT NULL,
`storyid` int(15) DEFAULT NULL,
`title` varchar(255) DEFAULT NULL,
`date` date DEFAULT NULL,
`details` longtext,
`category` varchar(255) DEFAULT NULL,
`author` varchar(110) DEFAULT NULL
)
It depends on the exact format of your .csv file but for Windows .csv format I always use
LINES TERMINATED BY '\r\n'
also (again depending on the data) try
FIELDS ESCAPED BY '\\' TERMINATED BY '\t' OPTIONALLY ENCLOSED BY '\"'
If you're unsure of the exact nature of the data sometimes it is better to view it in hexadecimal to see how the lines are really terminated. I use Hexedit - http://www.hexedit.com/
Hope this helps.
Dermot
Like I said in the comments you can use '/r/n' for a new line.
However your csv file contains only 1 column, namely a full line of text.
That is probably also why only the first table column is filled and the rest is null.
LOAD DATA LOCAL INFILE 'c:/news.csv'
INTO TABLE news
COLUMNS TERMINATED BY ','
LINES TERMINATED BY '\r\n'
(url, storyid, title, date, details, category, author)
This worked.
Turned out that even though it looks tab separated, it is comma separated. Dermot was right that you need to view it in hexadecimal view to see how it is really deliminated.

Fill MySQL table with the data from CSV file

There is a CSV file with the following data:
1;8-25-2010;0:05;210;4
2;8-25-2010;2:45;412;5
3;8-25-2010;3:40;300;3
4;8-25-2010;4:45;226;6
5;8-25-2010;5:20;206;4
6;8-25-2010;5:25;216;3
And there is MySQL Table:
CREATE TABLE IF NOT EXISTS `Schedule` (
`ID` SMALLINT NOT NULL AUTO_INCREMENT,
`Num` INT(10),
`PlannedDate` DATE,
`PlannedTime` TIME NOT NULL,
`resQty` INT(3) NOT NULL,
`stID` VARCHAR(10) NOT NULL,
PRIMARY KEY (`ID`),
FOREIGN KEY `stID` (`stID`) REFERENCES Stands (`stID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
Now I need to fill this table with the data from CSV file. For this I'm using the following code:
TRUNCATE TABLE testDB.Schedule;
LOAD DATA LOCAL INFILE 'C:\\temp\\Input.csv'
INTO TABLE testDB.Schedule FIELDS TERMINATED BY ';' ENCLOSED BY '"'
LINES TERMINATED BY '\r\n' (Num,PlannedDate,PlannedTime,stID,resQty);
But the error message says that "Data truncated for column PlannedDate at row1", ErrorNr. 1265. The same error message for all rows.
The date has the wrong format, it should be 2012-01-19
If you are stuck with that date format (which is not the MySQL default), you can load those dates into a temporary variable and then convert it to a date, like this:
TRUNCATE TABLE testDB.Schedule;
LOAD DATA LOCAL INFILE 'C:\\temp\\Input.csv'
INTO TABLE testDB.Schedule FIELDS TERMINATED BY ';' ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
(Num,#PlannedDate,PlannedTime,stID,resQty)
SET PlannedDate = STR_TO_DATE(#PlannedDate,'%m-%d-%Y');
in this line:
(Num,PlannedDate,PlannedTime,stID,resQty);
the two last parameters are reversed: stID,resQty
it SHOULD be:
resQty, stID
This is why you are getting a truncation error.