maintenance.c
/* confirm the existance of database items.db and database table clothing */
confirm the existance of database items.db and database table clothing
confirm the existance of database items . db and database table clothing
int confirm_tables()
{
    sqlite3 *db;
    int rc = sqlite3_open_v2("items.db", &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
    if (rc)
    {
        fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(db));
        return 1;
    }
    sqlite3_stmt *stmt;
    char *confirm_clothing = "SELECT name "
                             "FROM sqlite_master "
                             "WHERE type='table' AND name='clothing';";
    rc = sqlite3_prepare_v2(db, confirm_clothing, -1, &stmt, NULL);
    if (rc != SQLITE_OK) 
    {
        printf("error: %s\n", sqlite3_errmsg(db));
        return 1;
    }
    rc = sqlite3_step(stmt);
    if (rc != SQLITE_ROW) 
    {
        char *clothing = "CREATE TABLE clothing ("
                         "name          CHAR(100)  NOT NULL PRIMARY KEY, "
                         "days_passed   INT        NOT NULL, "
                         "coldest_t     INT        NOT NULL, "
                         "warmest_t     INT        NOT NULL, "
                         "layer_value   INT        NOT NULL);";
        sqlite3_reset(stmt); 
        rc = sqlite3_prepare_v2(db, clothing, -1, &stmt, NULL);
        if (rc != SQLITE_OK)
        {
            printf("error: %s\n", sqlite3_errmsg(db));
            return 1;
        }
        rc = sqlite3_step(stmt);
    }
    sqlite3_finalize(stmt);
    sqlite3_close(db);
    return 0;
}
int confirm_tables ( ) { sqlite3 * db ; int rc = sqlite3_open_v2 ( " " , & db , SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE , NULL ) ; if ( rc ) { fprintf ( stderr , " \n " , sqlite3_errmsg ( db ) ) ; return 1 ; } sqlite3_stmt * stmt ; char * confirm_clothing = " " " " " " ; rc = sqlite3_prepare_v2 ( db , confirm_clothing , -1 , & stmt , NULL ) ; if ( rc != SQLITE_OK ) { printf ( " \n " , sqlite3_errmsg ( db ) ) ; return 1 ; } rc = sqlite3_step ( stmt ) ; if ( rc != SQLITE_ROW ) { char * clothing = " " " " " " " " " " " " ; sqlite3_reset ( stmt ) ; rc = sqlite3_prepare_v2 ( db , clothing , -1 , & stmt , NULL ) ; if ( rc != SQLITE_OK ) { printf ( " \n " , sqlite3_errmsg ( db ) ) ; return 1 ; } rc = sqlite3_step ( stmt ) ; } sqlite3_finalize ( stmt ) ; sqlite3_close ( db ) ; return 0 ; }
confirm the existance of database items.db and database table clothing
confirm the existance of database items . db and database table clothing
/* flag SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE will create database if
    it does not yet exist */ // confirm query  /* if return code is not 101 then table does not yet exist. Create it */ /* PRIMARY KEY implies UNIQUE but in SQLite does not imply NOT NULL */ //NEW post test | rc = //printf("attempt to create: %d\n", rc);