On 12 Feb 2006 12:53:50 -0800, in mailing.database.mysql
sp********@gmail.com
<11**********************@z14g2000cwz.googlegroups .com> wrote:
| Hello,
|
| I am new to MySQL. I am trying to load from a text file to a table,
| and am having problems
| with the fields in the text file being translated to the correct fields
| in the TABLE. Right now,
| I have a tab between each field in the text file, something like:
|
| Chicago USA 2,500,000
| New York USA 5,000,000
| Madrid Spain 2,000,000
|
| My question: Can I use something else to differentiate between fields,
| like a coma?
| This would greatly help me.
|
| Thank you for your time.
When importing data, from any source, I place the data into a
temporary table, massage the data to the way I want it and then
transfer this data into my main table(s).
Setup:
create a temp table:
create table tmpImportTbl {
city varchar(100),
country varchar(100),
population varchar(100)
} type=MYISAM;
Why use varchar for the population? This is separated by commas. As
Bill has pointed out, they will be truncated so we need to store
whatever value into a text field.
Next the import:
LOAD DATA LOCAL INFILE 'c:\\temp\\population.txt' INTO TABLE
tmpImportTbl FIELDS TERMINATED BY '\t' ENCLOSED BY '"' LINES
TERMINATED BY '\n' IGNORE 1 LINES;
http://dev.mysql.com/doc/refman/5.0/en/load-data.html
This is saying:
get the file that is located within your c:\temp folder (change this
to where ever you save your file to)
place the data into the tmpImportTbl table
The file has the following format:
a tab character seperates the field values
(optionally) the values are enclosed in double quotes
when a linefeed character is found, start a new record
ignore the first line of the file (because it is an information row
i.e. the fields names).
*ALWAYS* check the mysql message after it has completed the process.
Make sure the number of records imported match the number of lines
within your text file. If they do not match then you need to empty the
tmpImportTbl and tweak the import parameters.
All being well the data should now be in the tmpImportTbl table.
Now to correct the population information. This is easily done by
removing the commas:
UPDATE tmpImportTbl set population=replace(population,',','');
Now to transfer the corrected data into the main table:
INSERT INTO mainTable (city,country,population) select city, country,
population from tmpImportTbl;
The main table structure might be:
create table mainTable {
city varchar(100),
country varchar(100),
population bigint(20) unsigned default '0'
/*
other fields defined here
*/
} type=MYISAM;
Clear out the temp table:
truncate table tmpImportTbl;
---------------------------------------------------------------
jn******@yourpantsyahoo.com.au : Remove your pants to reply
---------------------------------------------------------------