473,474 Members | 1,348 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Loading a file into a TABLE



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.

-D

Feb 12 '06 #1
4 2240
<sp********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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?


Certainly. You should read about the MySQL command LOAD DATA INFILE.
http://dev.mysql.com/doc/refman/5.0/en/load-data.html

For instance:
LOAD DATA INFILE 'textfile.txt' INTO TABLE databasename.tablename
FIELDS TERMINATED BY ','

Note, however, that your data already have commas in them. If you need to
have commas in the values in addition to being used as field terminators,
you must escape the literal commas. The default escape character is a
backslash (\). For example:

Chicago,USA,2\,500\,000
New York,USA,5\,000\,000
Madrid,Spain,2\,000\,000

Also note that MySQL doesn't understand numbers with thousands separators as
integers. You can input those values into string fields, but not numeric
fields. In the example above, it'll truncate the numbers and insert them as
integers 2, 5, and 2 respectively.

Regards,
Bill K.
Feb 12 '06 #2
sp********@gmail.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?
Do you have spaces in the second field?? or can you provide more sample
data?

for you current data(without spaces in the middle field) I can do(Under
Windows):

perl -alne "$,='#';print qq(@F[0..$#F-2]),@F[-2,-1]" mytb > mynewtb

or if all lines in your table are fixed-width-field records,

perl -alne "$,='#'; print unpack('A10A8A*',$_)" mytb > mynewtb

where 10 and 8 are the number of characters in your first and second
field, you need to count by yourself..

=======result=========
Chicago#USA#2,500,000
New York#USA#5,000,000
Madrid#Spain#2,000,000
====================
If you are under Linux, exchange all double quotes with single quotes
in the above command lines:

then under mysql:
LOAD DATA LOCAL INFILE 'D:\\mynewtb' INTO TABLE mydb.mytb FIELDS
TERMINATED BY '#' LINES TERMINATED BY '\r\n';

Best,
Xicheng
This would greatly help me.

Thank you for your time.

-D


Feb 13 '06 #3
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
---------------------------------------------------------------
Feb 13 '06 #4
sp********@gmail.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.

-D


The general term for this is ETL (Extract/Transform/Load).. so this would be a
two step process if you want to use a number/integer datatype for the
"population" field - (will make it much easier do execute math computations from
the database)

I am "assuming" that the field seperator in this case is a TAB character. If
this is so, then I would create a procedure that would:
1) EXTRACT DATA
2) TRANSFORM DATA (if necessary)
a) read each record
b) replace the TAB-delimiter with another meta-character like "~".
see http://us3.php.net/str_replace
c) replace the commas in the third field with "" (remove them)
again - see: http://us3.php.net/str_replace
d) write the file to a "transformed" file
3) LOAD "transformed" file into the database.

Or - you could re-extract the data and put it into a more readable format and
then just load it into the database.

Example:
Chicago~USA~2500000
New York~USA~5000000
Madrid~Spain~2000000

YMMV.

--
Michael Austin.
Consultant
Donations welcomed. Http://www.firstdbasource.com/donations.html
:)
Feb 13 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Demetris | last post by:
Hello there! I have a table (table1) and one of its columns DATA_X is CHAR(660) I will create another table (table2) similar to the above but i will replace the DATA_X column with several columns...
4
by: Ram | last post by:
We are trying to load text tab delimited files into SQL server using informatica 5.1 version. The text files are FTPed to a local server and then files are moved to applicable folder before...
0
by: Deep Purple | last post by:
I started with the following error... ------------------------------------------------------- An error occured while loading the schema with TargetNamespace 'http://www.w3.org/2001/XMLSchema'...
5
by: John Richardson | last post by:
I've been bothered for some time about my DataGrid not populating my rows very quickly. I have about 10K rows loading into the grid. I create a datatable dt with 2 columns, an ID and a display. ...
4
by: blackhawk | last post by:
I need to build a web page that has to potentially display a large amount of data in two grids on the same page. The HTML file with all of the formatting is about 7MB in size. This is too large...
0
by: Terry Olsen | last post by:
I wrote the following code to load a DataTable with a CSV file. My problem is: If I include the line "MyTable.Clear()" before I start loading the Table, I'm presented with the 2nd line of my CSV...
10
by: eholz1 | last post by:
Hello Members, I am setting up a photo website. I have decided to use PHP and MySQL. I can load jpeg files into the table (medium blob, or even longtext) and get the image(s) to display without...
5
by: yeoj13 | last post by:
Hello, I have a db2load script I'm using to populate a large table. Ideally, my target table is required to have "Not Null" constraints on a number of different columns. I've noticed a ...
20
by: Nickolai Leschov | last post by:
Hello all, I am programming an embedded controller that has a 'C' library for using its system functions (I/O, timers, all the specific devices). The supplied library has .LIB and .H files. ...
3
by: Leighya | last post by:
Im currently working on this xml file but when i load it to Mozilla, i got an error "Error Loading Stylesheet: Xpath parse failure: invalid variable name" It loads on IE properly. Only with the...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.