LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   Importing a csv file into a mysql database (https://www.linuxquestions.org/questions/linux-server-73/importing-a-csv-file-into-a-mysql-database-755999/)

kaplan71 09-17-2009 03:02 PM

Importing a csv file into a mysql database
 
Hi there --

I recently created a database with one table in it. The table, in turn has three fields. Listed below are the characteristics of the fields:

Quote:

Field name Type Allow nulls? Key
Backup varchar(50) No Primary
TapeID varchar(20) No None
Description varchar(50) Yes None
I want to import into the database a csv file containing the pertinent information. An excerpt of the file is the following:

Quote:

LTO-4 RadOnc Servers DQH137L4 Radiation Oncology Servers
LTO-4 Research DQH141L4 Radiation Oncology Research Systems
LTO-4 Xio Systems DQH138L4 Xio Workstations
I ran a SQL command via the Webmin utility to import the csv file into the database, and there were no error messages displayed. However, when I try to view the data, there is none to be seen.

The version of MySQL that is being used here is 5.0.45. Does anyone have an idea as to why this is occurring, and how I can correct it? Thanks.

forrestt 09-17-2009 03:07 PM

There aren't any commas in the excerpt, so how is this a comma separated value (csv) file?

Forrest

pliqui 09-17-2009 03:41 PM

CVS File
 
Example of a CVS file

Code:

Field        Type       
id        int(11)       
income  varchar(20)
time        int(11)
rate        double
min_ammount        int(11)
max_ammount        double
fc        int(11)

Code:

"2";"2.800,1 - 5.474";"30";"6.61";"0";"214055.75";"25"
"3";"5474,1 - 7.000";"30";"8.55";"0";"271858.95";"30"
"4";"7.000,1 - 10.120";"25";"10.5";"0";"321548.25";"30"
"5";"10.120,1 - 16.560";"25";"12.44";"0";"457509.38";"30"
"6";"16.560,1 - 23.000";"20";"14.39";"0";"542477.17";"30"
"1";"0 - 2.800";"30";"4.66";"0";"108477.55";"20"

Hope it helps

forrestt 09-17-2009 04:04 PM

Actually, that is a semi-colon separated value file (and I think you meant csv, not cvs which is a version control system). Also, int's and double's wouldn't have quotes. A cvs, or comma separated value, file would look like:

Code:

2,"2.800,1 - 5.474",30,6.61,0,214055.75,25
3,"5474,1 - 7.000",30,8.55",0,271858.95,30
4,"7.000,1 - 10.120",25,10.5,0,321548.25,30
5,"10.120,1 - 16.560",25,12.44,0,457509.38,30
6,"16.560,1 - 23.000",20,14.39,0,542477.17,30
1,"0 - 2.800",30,4.66,0,108477.55,20

Of course, the commas in the second field may cause issues.

HTH

Forrest

pliqui 09-17-2009 04:14 PM

Yes, it's a semi-colon format, didn't change it in phpmyadmin when i did the export, but is the same logic.

And sorry, was CSV not CVS, typo there :D. I'm from venezuela so format number here is xxx.yyy,zzzz, meaning like one thousand would be 1.000,00

chrism01 09-17-2009 09:03 PM

@forrest
Quote:

A cvs (!), or comma separated value, file would look like:
What's that quote about correcting others for a mistake you also make ;)
(do as I say, not as I do, iirc)

Anyway, to be serious:
@pliqui : show us a few lines of the actual(!) data file, the SQL cmd/msgs and use a desc cmd on the table.
If you had no errors(!), but no data appears, I'd say you used the wrong table name or schema (db) name.

Wim Sturkenboom 09-18-2009 07:19 AM

pliqui is not the one who posted the question. I think he/she only tried to explain

I admit, I was also a bit confused ;)

forrestt 09-18-2009 08:29 AM

chrism01, I guess typing cvs and csv so many times made my fingers confused (what's that thing about bedeg aale to rsed sasefkeig as ladg as tke fiast and ladt leasdrs are corsdesct?)

Anyway, pliqui isn't the one w/ the question or the csv (hey, I got it right that time) file, kaplan71 is. However, (s)he doesn't seem to be interested in the thread, so the entire exercise seems moot at this point.

Forrest

kaplan71 09-18-2009 09:45 AM

Hi there --

Sorry about the delay in getting back to you all. I am definitely interested in the thread, so if I was not prompt in replying, it was completely accidental.

I was using a web interface to do the job, and that did not seem to work.

The next step was to try the command listed below:

Code:

load data local infile '/home/ahk/August2009-1.csv' into table offsite_archive
fields terminated by ','
enclosed by '"'
lines terminated by '\n'
(Backup, TapeID, Description);

The end-result was the following:

Quote:

Failed to execute SQL : SQL load data local infile '/home/ahk/August2009-1.csv' into table offsite_archive fields terminated by ',' enclosed by '"' lines terminated by '\n' (Backup, TapeID, Description); failed : File '/home/ahk/August2009-1.csv' not found (Errcode: 2)
I am not readily with SQL commands, so I am guessing there is an error with my syntax.

Wim Sturkenboom 09-18-2009 11:01 AM

Be aware that the filename is case sensitive. Further, because you're using local, the mysql client looks on the machine on which it's running for the file and it needs to be able to access it (permissions?).

kaplan71 09-18-2009 11:30 AM

Hi there --

I have made some progress. Once I changed the permissions on the file, I ran the commands again. This time, the first line of the file was imported into the database, but not the remaining lines.

Just to recap: The file is saved as a csv file, and can be opened within Microsoft Excel, or OpenOffice Spreadsheet. Any ideas? Thanks.

Wim Sturkenboom 09-18-2009 12:08 PM

The lines might not be terminated by '\n'. So it reads the whole file as one line and there's only space for a limited number of fields and the rest is ignored.

kaplan71 09-18-2009 01:20 PM

Hi there --

The removal of the "lines terminated by '\n'" code made the difference.

Thanks again to everyone for the help.

pliqui 09-18-2009 06:13 PM

Sorry i couldn't answer before but had some production problems.

I'm at home right now and don't have the desc of the table, but what i did was an export of the table with phpmyadmin and saved the file as csv. Phpmyadmin allows to change the paramater for the separator, so could be ',', ';','|', etc. the default one is ';'.

But glad you could resolve your problem.

Isaac Abouganem


All times are GMT -5. The time now is 02:08 AM.