Skip to content

API kr_open

JoungKyun Kim edited this page Jul 13, 2016 · 7 revisions

NAME

kr_open - open libkrisp database

SYNOPSIS

#include <krisp.h>
typedef struct db_argument {
    char            err[1024];
    char            database[256];
#ifdef HAVE_PTHREAD_H
    pthread_mutex_t mutex;
#endif
#if defined(HAVE_LIBSQLITE3)
    sqlite3 *       c;      // db resource
    sqlite3_stmt *  vm;     // sqlite vm
#else
    sqlite *        c;      // db resource
    sqlite_vm *     vm;     // sqlite vm
#endif
    const char **   rowdata;
    const char **   colname;
    char *          table;
    char *          old_table;
#if defined(HAVE_LIBSQLITE)
    char *          dberr;  // vm error message
#endif
    int             rows;   // vm rows
    int             cols;   // number of columns

    time_t          db_time_stamp; // database mtime stamp
    time_t          db_stamp_checked; // record db mtime check time
    //db mtime stamp check interval. If value is 0, don’t check
    time_t          db_time_stamp_interval;

    short           verbose;
#ifdef HAVE_PTHREAD_H
    short           threadsafe;
#endif
    short           r;      // execute result code
    short           final;  // force finalize
} KR_API;

bool kr_open (KR_API ** db, char * path, char * err);
bool kr_open_safe (KR_API ** db, char * path, char * err);

DESCRIPTION

kr_open api used to open database handle. kr_open api has three arguments. First argument is KR_API structure which is used database connection, second is sqlite database file path. Third argument has error messages when kr_open api is failed.

KR_API structure is defined on krispcommon.h, and used as follows.

KR_API *db;
char *err[1024];
if ( kr_open (&db, NULL, err) == false ) {
    ..
}

If you call kr_open_safe api instead of kr_open, thread lock is create on calling kr_search api. For this issue, see also thread lock documents of libkrisp source code.

RETURN VALUE

On success, return true, otherwise return false.

EXAMPLE

#include <krisp.h>

int main (void) {
    KR_API * db;
    KRNET_API isp;
    char * addr = "kns.kornet.net";
    char * database = NULL;
    char err[1024];

    if ( kr_open (&db, database, err) == false ) {
        fprintf (stderr, "ERROR Connect: %s\n", err);
        return 1;
    }

    isp.verbose = false;
    // defined safecpy_256 in krispapi.h
    safecpy_256 (isp.ip, addr);
    if ( kr_search (&isp, db) ) {
        printf ("ERROR: %s\n", isp.err);
        kr_close (&db);
        return 1;
    }

    kr_close (&db);

    /*
     * long2ip is include libipcalc.
     * long2ip is returned in a statically allocated buffer, which
     * https://github.com/Joungkyun/libipcalc/wiki/API-long2ip
     * subsequent calls will overwrite. For thread safe, use long2ip_r.
     */
    printf ("%-15s %-15s %-20s", ip, isp.ip, isp.icode);
    printf ("%-15s ", long2ip (isp.start));
    printf ("%-15s %s", long2ip (isp.end), isp.iname);

    return 0;
}

AUTHORS

JoungKyun.Kim <http://joungkyun.github.com>

BUGS

Report to https://github.com/Joungkyun/libkrisp/issues

COPYRIGHT

Copyright (c) 2016 JoungKyun.Kim <http://oops.org>

This api is follows LGPL 2.1

SEE ALSO

kr_close(3), kr_search(3)

Clone this wiki locally