473,395 Members | 1,466 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

LOAD DATA INFILE - I'm stumped

I am able to extract data from our library database with specialized tools developed by the vendor. I can extract to a tab delimited file, csv, etc. I extract titles, authors, purchase orders and that kind of stuff. The files are created using wordpad, notepad (windows system)

However, if I extract to a csv file, the last data field has a comma at the end. For example:

PO-123,QA 123,Learning MySQL, Miller, John,

I can't figure out how to load the file to a table and take into account the comma at the end.

If I create a tab delimited file, it appears a tab is placed at the end of the line but I can't actually see it, I'm assuming since a comma is placed at the end of a CSV file. I tried loading using various lines terminated by variations and nothing with no luck, except if I don't get a syntax error the data is whackie, and not in the correct place.

I have attempted to load the files about 22 thousand times now, with no luck.
My first field in the table is the primary key auto_increment. I added NULL to the front of every line, but it doesn't appear to work as expected.

I have spend many hours searching websites for the answer. I just don't understand how to load a file so it takes into account that the first column in the table is a primary key with auto_increment and the extra tab or comma at the end of each line.

Thanks.
Jun 19 '07 #1
6 5468
nomad
664 Expert 512MB
I am able to extract data from our library database with specialized tools developed by the vendor. I can extract to a tab delimited file, csv, etc. I extract titles, authors, purchase orders and that kind of stuff. The files are created using wordpad, notepad (windows system)

However, if I extract to a csv file, the last data field has a comma at the end. For example:

PO-123,QA 123,Learning MySQL, Miller, John,

I can't figure out how to load the file to a table and take into account the comma at the end.

If I create a tab delimited file, it appears a tab is placed at the end of the line but I can't actually see it, I'm assuming since a comma is placed at the end of a CSV file. I tried loading using various lines terminated by variations and nothing with no luck, except if I don't get a syntax error the data is whackie, and not in the correct place.

I have attempted to load the files about 22 thousand times now, with no luck.
My first field in the table is the primary key auto_increment. I added NULL to the front of every line, but it doesn't appear to work as expected.

I have spend many hours searching websites for the answer. I just don't understand how to load a file so it takes into account that the first column in the table is a primary key with auto_increment and the extra tab or comma at the end of each line.

Thanks.
In Mysql I would first make a db. Make tables which has all fields that you want.
Once the db is created select your Table. From there Select the Insert tab. You should see all your fields for that Table. Notice the Import Tab on the top right. Select it
You should see a File to import. Select the Browser and find your CSV file.
Next you will see Format of imported Files.
Select CSV button.
A list of CSV options will appear.
Fields terminated by should be;
You might have to play with the settings to get it right. If the data is in the wrong place you will need to truncate the table. Then go back and try the other settings.

Note your CSV file the data will have to look like this
PO-123,QA 123,Learning MySQL, Miller, John;
Notice the ; this is the terminated field.
Hopefully your files are not to big. If you have Excel and know how to use it I would import all my data into Excel. Call the comma as col.
Replace all the comma and the end with ;.
Make a new row for your PK and leave it black for Null.
export the data to a CSV file.

good luck
nomad
Jun 19 '07 #2
nomad
664 Expert 512MB
In Mysql I would first make a db. Make a table which has all your fields that you need.
Once the db is created select your Table. From there Select the Insert tab. You should see all your fields for that Table. Notice the Import Tab on the top right. Select it
You should see a File to import. Select the Browser button and find your CSV file.
Next you will see Format of imported Files.
Select CSV button.
A list of CSV options will appear.
Fields terminated by should be ;
You might have to play with the settings to get it right You will need the comma , . If the data is in the wrong place you will need to truncate the table. Then go back and try the other settings.

Note your CSV file the data will have to look like this
PO-123,QA 123,Learning MySQL, Miller, John;
Notice the ; this is the terminated field.
Hopefully your files are note to big. If you have Excel and know how to use it I would import all my data into Excel. Call the comma as col.
Replace all the comma and the end with ;. export the data to a CSV file.

good luck
nomad
please disregard this one. I was updating the info in the other message. Made this one by mistake
Jun 19 '07 #3
nomad
664 Expert 512MB
Once you have your table I think you can get your PK set up. I will have to look that one up for you. Or you can set it up in Excell with a number.

nomad
Jun 19 '07 #4
Atli
5,058 Expert 4TB
You can choose the columns you want to insert the data into.
By that I mean.

Say I have this data in a file:
(Note that the line numbers are not a part of it!)
Expand|Select|Wrap|Line Numbers
  1. var11, var12, var13
  2. var21, var22, var23
  3. var31, var32, var33
  4.  
And I vant to insert it into this database:
Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE something
  2. (
  3.   ID SERIAL Primary key,
  4.   Var1 VARCHAR(255) NOT NULL,
  5.   Var2 VARCHAR(255) NOT NULL,
  6.   Var3 VARCHAR(255) NOT NULL
  7. )
  8.  
I can use the LOAD DATA command like this:
Expand|Select|Wrap|Line Numbers
  1. LOAD DATA INFILE 'backup.txt'
  2. INTO TABLE something
  3. FIELDS TERMINATED BY ','
  4. (Var1, Var2, Var3);
  5.  
The ID column will be populated automaticly as it is an AUTO_INCREMENT field, as will all fields that have a default value and are not included.

As for the extra commas, you could write a simple script in most programming languages to remove that. It's just a matter of replacing all ',\n' accurances with '\n'.

Edit:
The reason why you can not write NULL in an AUTO_INCREMENT field is that NULL is not treated as a value. It can't even be used in boolean expressions.
The value 'NULL' which is imported from your file is treated as a string value, and can therefore not be converted into an integer.

You can, however, put the number 0 into an auto increment field.
That will be converted into the INT value 0, which is treated as NULL and a new value will be generated.

So my previous data could have been formatted like this, and I could have skipped listing the columns.
Expand|Select|Wrap|Line Numbers
  1. 0, var11, va12, var13
  2. 0, var21, var22, var23
  3. 0, var31, var32, var33
  4.  
Jun 20 '07 #5
Atli
5,058 Expert 4TB
P.S.
MySQL's "SELECT * INTO OUTFILE" statement does not add the extra comma to the end of the line.
If your application is adding extra delimiters you might want to contact who ever makes it and ask for a fix.

From where are you extracting this info btw?
Jun 20 '07 #6
Atli
5,058 Expert 4TB
Here is an idea, how you can fix the extra comma thing.

You can add a column to your table, which will take in the empty string the extra comma makes, and then delete it.

Like so:
Expand|Select|Wrap|Line Numbers
  1. ALTER TABLE myTbl
  2. ADD COLUMN del VARCHAR(10) NOT NULL Default '';
  3.  
Then add your data.

And finally remove the column
Expand|Select|Wrap|Line Numbers
  1. ALTER TABLE myTbl
  2. DROP COLUMN del;
  3.  
Jun 20 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Pieter Van Waeyenberge | last post by:
Hello i have most recent php & mysql versions running on a linuxmachine i want to use LOAD DATA LOCAL INFILE and pass it from php to mysql. - the file i load is given with full path and the...
14
by: Bruce A. Julseth | last post by:
When I execute this SQL statement in my PHP code, I get an error "File '.\Address.txt' not found (Errcode: 2)" $File = addslashes(".\Address.txt"); $SQL = "Load Data InFile \"" . $File . "\"...
3
by: Otto | last post by:
Hello to all I have a problem how to write correctly a PHP string. I did the following import with the help of PHPMyAdmin: Nombre d'enregistrements insérés : 364 (traitement: 0.0553 sec.)...
3
by: hall | last post by:
I have a problem with my design of a templatized class. I'm trying to figure out how to load and save the data inside it, but can't. My class looks like this ------------------------------------...
0
by: Donald Tyler | last post by:
Then the only way you can do it that I can think of is to write a PHP script to do basically what PHPMyAdmin is trying to do but without the LOCAL in there. However to do that you would need to...
1
by: Jami Bradley | last post by:
HI all, For the past several months we have been using LOAD DATA LOCAL INFILE to bulk load tables within Perl modules. Recently, someone thought it would be a good idea to upgrade the Solaris...
1
by: Ray in HK | last post by:
What are the differences between LOAD DATA INFILE and LOAD DATA LOCAL INFILE ? I found some web hosting company do not allow using LOAD DATA INFILE but allow LOAD DATA LOCAL INFILE. The reason...
3
by: nsh | last post by:
mailing.database.mysql, comp.lang.php subject: does "LOAD DATA" EVER work?!? I've tried EVERYTHING! version info: my isp is running my web page on a linux box with php ver. 4.4.1 according to...
0
by: lanesbalik | last post by:
hi all, right now i'm trying to migrate from db2 running under linux to mysql v5.1. i manage to export out the db2 structure & data into a del (ascii) file. but when i try to load the data...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.