Solved

Searching + Deleting from databases in C?

Posted on 2004-04-07
6
164 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
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.

747 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

11 Experts available now in Live!

Get 1:1 Help Now