Jump to content

Load data infile messing up the numbers


Go to solution Solved by Barand,

Recommended Posts

I'm using the code below to load data from CSV to mysql, but the numbers imported are getting all messed up. I have mysql fields formated as decimal (10,2). Also, tried real, double, float, but nothing is helping.

 

For example, line I'm importing from CSV is like this:

967 54 71-66 872,64 1.679,20

2.099

Data I get in mysql is like this:

967 54 71-66 87264.00 1.68 kn 0.00 kn 0.00 kn 0.00 kn

 

How can I format the numbers to get them in mysql in exact shape like they are in CSV file?

 

	$q_load =   'LOAD DATA LOCAL INFILE "upload/'.$_FILES['filename']['name'].'"
				INTO TABLE kalkulacija_import_povijest_stavke
				FIELDS TERMINATED BY ";" OPTIONALLY ENCLOSED BY ""
				LINES TERMINATED BY "\r"
				IGNORE 1 LINES
				(kataloski_broj, @prosjecna_nc, @vpc, @mpc, @pvpc, @pmpc)
				SET 
				id_povijest = '.$id_cjenika.' , 
				godina_pov = '.$godina.' ,
				prosjecna_nc = REPLACE (@prosjecna_nc, "," , "") ,
				vpc = REPLACE(@vpc, "," , "") ,
				mpc = REPLACE(@mps, "," , "") ,
				pvpc = REPLACE(@pvpc, "," , "") ,
				pmpc = REPLACE(@pmpc, "," , "")
				';

 

Link to comment
https://forums.phpfreaks.com/topic/275339-load-data-infile-messing-up-the-numbers/
Share on other sites

Barand,

 

I'm using the same code on some other CSV files and is working properly, but on this CSV where I have different number formating is not working. The code is working, but number formating is not good. I'm getting article number and one price column in, but the second column is getting strange number and after that nothing.

 

I don't know why number formating is wrong? Do you have any suggestions how could I test what is failing?

Barand,

 

I don't get any result from "SHOW CREATE TABLE kalkulacija_import_povijest_stavke". I have attached a CSV I'm trying to import so you can take a look and maybe see what is the problem...

NC i PC 2009.zip

Edited by budimir

SHOW CREATE TABLE xxxx

 

should give something like this example

 

CREATE TABLE `xxxx` (
  `locId` int(10) unsigned NOT NULL,
  `country` char(2) COLLATE latin1_general_ci NOT NULL,
  `region` char(2) COLLATE latin1_general_ci NOT NULL,
  `city` varchar(50) COLLATE latin1_general_ci DEFAULT NULL,
  `postalCode` char(5) COLLATE latin1_general_ci NOT NULL,
  `latitude` float DEFAULT NULL,
  `longitude` float DEFAULT NULL,
  `dmaCode` int(11) DEFAULT NULL,
  `areaCode` int(11) DEFAULT NULL,
  PRIMARY KEY (`locId`),
  KEY `country` (`country`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci 
Edited by Barand

Here it is.

 

CREATE TABLE `kalkulacija_import_povijest_stavke` (  
`id` int(255) NOT NULL AUTO_INCREMENT,  
`id_povijest` int(255) NOT NULL,  
`vrijeme` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP,  
`godina_pov` year(4) NOT NULL,  
`kataloski_broj` varchar(255) NOT NULL,  
`prosjecna_nc` decimal(10,2) NOT NULL,  
`vpc` decimal(10,2) NOT NULL,  
`mpc` decimal(10,2) NOT NULL,  
`pvpc` decimal(10,2) NOT NULL,  
`pmpc` decimal(10,2) NOT NULL,  
PRIMARY KEY (`id`)) 
ENGINE=MyISAM 
AUTO_INCREMENT=47838 
DEFAULT CHARSET=latin1

I tried to format cells in CSV as text instead as number and than is importing OK. The problem there is I don't get decimal, only a round number.

 

So I guess it is a problem with number formating, but I don't know how to deal with it...

I'm completly lost now... Tried some conversions with decimal holders changing from . to , and other way. No success. Numbers which don't have comma in it are imported properly, but numbers that have comma in it get rounded to a very small number like 1.678,99 gets 1.68???

 

I don't get it. Why is it doing that?

I can't implement it in the code below. It displays error in mysql syntax. Can you show me how to use that function in this code?

 

	$q_load =   'LOAD DATA LOCAL INFILE "upload/'.$_FILES['filename']['name'].'"
				INTO TABLE kalkulacija_import_povijest_stavke
				FIELDS TERMINATED BY ";" OPTIONALLY ENCLOSED BY ""
				LINES TERMINATED BY "\r\n"
				IGNORE 1 LINES
				(kataloski_broj, prosjecna_nc, vpc, mpc, pvpc, pmpc)
				SET id_povijest = '.$id_cjenika.' , godina_pov = '.$godina.'';			
				
	mysql_query($q_load) or die (mysql_error());

This is the code I use, but it doesn't work. It inserts 0 to fileds.

 

	$q_load =   "LOAD DATA LOCAL INFILE 'upload/".$_FILES['filename']['name']."'
				INTO TABLE kalkulacija_import_povijest_stavke
				FIELDS TERMINATED BY ';' OPTIONALLY ENCLOSED BY ''
				LINES TERMINATED BY '\r\n'
				IGNORE 1 LINES
				(kataloski_broj, @prosjecna_nc, @vpc, @mpc, @pvpc, @pmpc)
				SET 
				id_povijest = ".$id_cjenika." , 
				godina_pov = ".$godina." , 
				prosjecna_nc = CONVERT(REPLACE(REPLACE('@prosjecna_nc', '.', ''), ',', '.'), DECIMAL(10,2)) ,
				vpc = CONVERT(REPLACE(REPLACE('@vpc', '.', ''), ',', '.'), DECIMAL(10,2)) ,
				mpc = CONVERT(REPLACE(REPLACE('@mpc', '.', ''), ',', '.'), DECIMAL(10,2)) ,
				pvpc = CONVERT(REPLACE(REPLACE('@pvpc', '.', ''), ',', '.'), DECIMAL(10,2)) ,
				pmpc = CONVERT(REPLACE(REPLACE('@pmpc', '.', ''), ',', '.'), DECIMAL(10,2))";			
				
	mysql_query($q_load) or die (mysql_error());

Barand,

 

I got it. Tried to experiment a bit, and this is the code that is working for me.

 

	$q_load =   "LOAD DATA LOCAL INFILE 'upload/".$_FILES['filename']['name']."'
				INTO TABLE kalkulacija_import_povijest_stavke
				FIELDS TERMINATED BY ';' OPTIONALLY ENCLOSED BY ''
				LINES TERMINATED BY '\r\n'
				IGNORE 1 LINES
				(kataloski_broj, @prosjecna_nc, @vpc, @mpc, @pvpc, @pmpc)
				SET 
				id_povijest = ".$id_cjenika." , 
				godina_pov = ".$godina." , 
				prosjecna_nc = CONVERT(REPLACE(REPLACE(@prosjecna_nc, '.', ''), ',', '.'), DECIMAL(10,2)) ,
				vpc = CONVERT(REPLACE(REPLACE(@vpc, '.', ''), ',', '.'), DECIMAL(10,2)) ,
				mpc = CONVERT(REPLACE(REPLACE(@mpc, '.', ''), ',', '.'), DECIMAL(10,2)) ,
				pvpc = CONVERT(REPLACE(REPLACE(@pvpc, '.', ''), ',', '.'), DECIMAL(10,2)) ,
				pmpc = CONVERT(REPLACE(REPLACE(@pmpc, '.', ''), ',', '.'), DECIMAL(10,2))";			
				
	mysql_query($q_load) or die (mysql_error());

Thank you very much for you're help!!! You really are saving my life...

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.