Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- C/Database Toolchest Library - name: <b>icreate_db</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
Name:       icreate_db

Purpose:    create a new database

Prototype:  Db_Obj *icreate_db(char *db_name,int blksize,char *field_names[]);

Header:     isam.h

Inputs:     db_name     - name of database to create
            blksize     - size of blocks in index or 0 for default
            field_names - array of pointers to field names for database

Description:
        icreate_db creates and opens a new database, which consists of a
        data file and its associated index file.  Both files use db_name
        as the main part of the filename; the data file has an extent of .db
        and the index file has an extent of .idx.  These extents are defined
        in the source file filename.c so that you may change them if you wish.
        The db_name may include a driver and/or path name, if you wish.
        You should not include an extent on the db_name; if you specify
        one, it is ignored.

        The blksize is the size of the blocks (nodes) in the index file.
        You can use 0 or DEFAULT_BLKSIZE to get the default block size.  If
        you choose a block size that is less than the minimum block size,
        it is an error.

        field_names defines the record format of the database.  It is an
        array of "argv-style" nul-terminated ASCII strings that names the
        fields in the order that the fields will appear in records for the
        database.  The last element of field_names must be the NULL
        pointer.

        icreate_db returns the handle for the DataBase object that you
        pass to other ISAM routines.  The database file initially contains
        only information and no data.

        icreate_db also makes the Physical Index for the new database.
        The file cursor (current record pointer) for the Physical Index
        is set to BOI (Beginning Of Index).  No other indexes are made
        automatically.  Use imkindex to make other indexes.

        icreate_db functions rather like iopen_db, except that a new
        database is created.  If there is an existing database by the given
        name, if it destroyed, and all the data that the old database
        contained is lost.

        icreate_db automatically calls isaminit (using default values
        for the arguments) if it hasn't been called yet.

        It is an error if either file for the database cannot be created.  It
        is an error if the database exists and is already open.

Returns:
        Database handle or NULL for error

Example:
#include    "isam.h"

char    *record_format[] = {
        "name",
        "phone number",
        "city",
        NULL
};

char    *record1[] = {
        "Johann",
        "(214) 555-7890"
        "Dallas"
};

char    *record2[] = {
        "Anna",
        "(303) 555-6543"
        "Silverton"
};

char    *record3[] = {
        "Peter"
        "555-2109"
        "Caspar"
};

char    *description1[] = {
        "name",
        NULL
};

char    *description2[] = {
        "city",
        "state",
        NULL
};

int main ()
{
    Db_Obj  *phone_db;

    phone_db = icreate_db ("phonbook", 0, record_format);
    if (phone_db == NULL) {
        iprterr();
        exit (1);
    }

    if (imkindex (phone_db, "name", description1) == OK)
        printf("Made name index\n");
    else {
        iprterr();
        exit (1);
    }

    if (imkindex (phone_db, "master", description2) == OK)
        printf("Made master index\n");
    else {
        iprterr();
        exit (1);
    }

    iaddrec (phone_db, NULL, record1);
    iaddrec (phone_db, NULL, record2);
    iaddrec (phone_db, NULL, record3);

    iclose_db (phone_db);
}

See Also: iopen_db isaminit iaddrec imkindex

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson