Solved

Searching + Deleting from databases in C?

Posted on 2004-04-07
6
165 Views
Last Modified: 2010-04-15
Hi,

I want to search a database/text file/master record by Surname, Post-code and/or Email Address, and also delete and entry from a master file yet i do not know how.

I know how to print ALL of the selected fields in a master file, yet not search, for example...


------------

               printf ("Address    : %s\n", contacts[i].address1);
                  printf ("             %s\n", contacts[i].address2);
                  printf ("             %s\n", contacts[i].address3);
                  printf ("PostCode   : %s\n", contacts[i].postcode);
                  printf ("Home Number: %s\n", contacts[i].homenumber);
                  printf ("Mob. Number: %s\n", contacts[i].mobnumber);
                  printf ("Email      : %s\n", contacts[i].email);

-------------

Any help/examples are appreciated

Ryan
0
Comment
Question by:ryb2001
  • 2
6 Comments
 
LVL 10

Accepted Solution

by:
Sys_Prog earned 125 total points
ID: 10774830
This seems to be a hoemwork sort of question

Hence cannot give u the code , but can Definitely help

U need to define a struct having the same format as that of a record in your file
Open the file [fopen()]
read one record at a time from the file  [fread()]
Compare the individual elemtnts of the read record using strcmp () OR other comparison techniques [depending on datatype of elements]

For Deletion,
U need to decide the strategy
U can either delete the whole record, In that case u need to somehow utilise the space emptied by the deleted record [either by inserting a new record if required OR moving forward by size of one record the data after the deleted record ]

U can also have logical deletion [maintaiing a delete flag with each record to indicate whether a particular record is deleted. This technique would prevent moving the data back and forth BUT leads to wastage of space in case u have many deletions. U can utilize the space occupied by deleted records by overwriting them at a later stage while inserting new records


Amit
0
 
LVL 9

Assisted Solution

by:ankuratvb
ankuratvb earned 125 total points
ID: 10775170
Besides Linear Search,if ur records are large in no. and u are likely to search quite often,
U can use binary search as well.

The problem u'll have is Binary search requires sorted records.
Now u can sort on one field in the record while storing.

So,the field on which u'll be searching the most,keep the records sorted on that field.

The advantage u gain by binary search is quite a lot.

For deletion also,Amit covered most of ur options.

U have to make the choice whether size is more imp. or speed .
If speed,u can use the delete flag and not physically delete the record.
Physical deletion can be done at peiodic intervals on use request.

If space is ur concern,then physical deletion and moving all subsequent records is the best option.

0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10775404
U can use fwrite() to write the entire structure i.e. one record in one statement rather than having to write one field at a time.


0
 
LVL 2

Expert Comment

by:anupvijay
ID: 10775512
Hello Ryan,

If you are comfortable in DB programming using C on windows then you can also try the ODBC route.
Even for text files there are Text drivers available where once you connect and open a datasource
it is very trivial to perform queries and searches.

The effort for writing parsing and searching logics would be less but then it has its own challenges.

Visit
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odbc/htm/odbcjetmicrosoft_desktop_database_drivers.asp

Enjoy
Anup
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

777 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