C Language Development Kit/Snippets/Database
From OptiWiki
The database functions that will be described in this section can be used to create and use databases with the following properties.
| Record size | Fixed (All records must have same length) |
|---|---|
| Support of sorted and unsorted databases | Both |
| Availability of search functions | Yes |
| Availability of sort functions | Yes |
| Possibility to add an index file to the database | Yes |
Contents |
Sources of all database functions
To be able to use the database functions, include the following header file and source file in your project.
| Header file | database.h (source) |
|---|---|
| Source file | database.c (source) |
Creating, opening and closing a database
The following functions can be used to create, open and close a database.
- CreateDatabase() Creates a new database file and opens it
- OpenDatabase() Opens a database file.
- CloseDatabase() Closes a database file that was opened or created.
Database properties
The following functions return general properties and information of the current state of the database.
- GetCurrentRecord() Gets the current record number.
- GetTotalRecords() Gets the total number of records that are stored in the database.
Read, write and delete records of a database
The following functions make it possible to read, write, delete and jump to every desired record of the database.
- GotoRecord() Jumps to a specific record in the database. (Sets the current record number.)
- ReadCurrentRecord() Reads the record where GetCurrentRecord() points to.
- ReadFirstRecord() Reads the first record in the database.
- ReadLastRecord() Reads the last record in the database.
- ReadPreviousRecord() Reads the record before the current.
- ReadNextRecord() Reads one record after current.
- DeleteRecord() Deletes a specific record in the database.
- WriteRecord() Appends or overwrites a record to the database.
Searching records in a database
The following functions can be used to search records in a database. Use BinarySearch() for sorted databases and use LineairSearch() for unsorted databases.
- BinarySearch() Searches in database for a
searchkey. Works only on sorted databases. - LineairSearch() Searches database for a
searchkey. Works on sorted and not sorted databases, but is slow.
Sorting records of a database
Depending on whether the database is already almost sorted or not and the amount of resources that may be used, the following sorting functions are available.
- InsertionSort() Sorts database with insertion sort algorithm. Fast for almost sorted databases.
- HeapSort() Sorts database with heapsort algorithm. Good performance on not sorted databases.
- QuickSort() Sorts database with quicksort algorithm. Very good performance on not sorted databases.
Creating and using index files
Sorted index files can be added to a database to be able to quickly search in unsorted databases that may not be sorted. It is also possible to add multiple sorted index to a single database to quickly search for different aspects of a record.
- CreateIndexFile() Creates an index file on a not sorted database.
- OpenIndexFile() Opens an index file
- SearchIndexFile() Searches for a searchkey in an indexed file.
- AddNewSearchkeyToIndex() Adds a new
searchkeyto an index file.
