Solved

passing data from text file in to mysql database?

Posted on 2002-07-24
3
213 Views
Last Modified: 2010-08-05
hai all,

I wanted to pass data from from txt file in to mysql db.
the data is testing results of few boards(tested) in which I have serial number of the board,few testing parameters,& result=Fail or Pass.

my requirement is as follows:-
1)I need to read each line of the data in txt file
2)then have to check whether that particular board already exists in the database
  if it already exists check for fail or pass status in db.
  if(fail)
    then the current line of data should overwrite the existing data in db.
  if(pass)
    then the current line of data should be added in to db.
3)this loop should be done until end of the txt file.

can I do this way of overwriting & adding data from text file line by line.
if possible can anyone suggest me the way to resolve it.

The problem now I feel I would face is, all the while I pass the whole data in to db by following query
 "load data infile '".$path."".$category.".tmp' into table ".$category." fields terminated by\",\";"

but if I wanted to add data & overwrite data(if any) line by line in to db how to go about it.

if any one could suggest me!!!!!!! pls kindly do that.

Thank u
manju





0
Comment
Question by:manju70
3 Comments
 
LVL 4

Expert Comment

by:lokeshv
ID: 7176355
ofcourse u can do it by this way...

and u know the algo ...u just ned to write the code.

read file line by line(by fgets or u can read the entire file into a array by file function ) n do as ur algo...

or if ur file is CSV file then u can fgetcsv also..

after that i think u can write the code...just if and else loops..


if get into any trouble please..post here...

Regards,
Lk
0
 

Author Comment

by:manju70
ID: 7176515
csv file meanssssss!!!!!!!
manju
0
 
LVL 40

Accepted Solution

by:
Richard Quadling earned 45 total points
ID: 7176689
CSV = Comma Seperated Variable.

These files are relative OK. The things to watch out for is having a ',' as part of the data.

e.g.

Board = GJ21
Test = a
Pass = True

In a CSV file, this could look like ...

GJ21,a,True

If the board name was GJ21,FF44, then the CSV file would look like ...

GJ21,FF44,a,True

The problem with this is that there is no immediate way of determining the position of the 3 fields as the CSV file has 4 fields in it.

Normally, you would have " around the text fields ...

"GJ21,FF44",a,True

This now works fine and most, if not all, CSV importers SHOULD deal with this correctly.

Another option is to use a Fixed length file.

In this format, you say that the board name is a maximum of 20 letters, the test is a max of 4 letters and the true/false is represented as T or F ...

12345678901234567890123451
GJ21,FF44           a    T
jlkdskfjsldfsdfj sdfXXXXXF

Other people use the TAB character to seperate the fields (a TABBED file). You COULD use an XML file too.

Many choices.

The easiest is probably the fixed length file as you simply hardcode the lengths. No need to worry about commas or quotes.



0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question