Solved

passing data from text file in to mysql database?

Posted on 2002-07-24
3
209 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:
RQuadling 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

760 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now