[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 178
  • Last Modified:

Searching + Deleting from databases in C?

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
ryb2001
Asked:
ryb2001
  • 2
2 Solutions
 
Sys_ProgCommented:
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
 
ankuratvbCommented:
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
 
ankuratvbCommented:
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
 
anupvijayCommented:
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

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!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now