Template09
Template09

Reputation: 164

sqlite3_prepare_v2 != SQLITE_OK

I wanna ask about my function below. It print NOT AVAILABLE when I call this function. Could you help me please??

    static sqlite3 *database = nil;
    static sqlite3_stmt *statement = nil;

    - (BOOL) findNews:(NSString *)caption{   
    const char *dbpath = [databasePath UTF8String];
    if (sqlite3_open(dbpath, &database) == SQLITE_OK)
    {
        NSLog(@"CAPTION ID : %@", caption);
        NSString *querySQL = [NSString stringWithFormat:@"SELECT * FROM dbase WHERE CONTENT_ID = \"%@\"", caption];
        const char *query_stmt = [querySQL UTF8String];

        if (sqlite3_prepare_v2(database, query_stmt, -1, &statement, NULL) == SQLITE_OK)
        {
            if (sqlite3_step(statement) == SQLITE_ROW)
            {
                return YES;
            }
            else{
                return NO;
            }
            sqlite3_reset(statement);
        }else{
            NSLog(@"NOT AVAILABLE");
        }
    }
    return nil;
}

Upvotes: 0

Views: 4837

Answers (2)

rakeshNS
rakeshNS

Reputation: 4257

sqlite3 open. "If the filename is an empty string, then a private, temporary on-disk database will be created. This private database will be automatically deleted as soon as the database connection is closed."

So the sqlite3_open will return SQLITE_OK, if the data base not copied into Documents directory. You should copy the data into Bundle and copy this data base to Documents directory.

-(void) checkAndCreateDatabase{

    BOOL success;

    NSFileManager *fileManager = [NSFileManager defaultManager];

    success = [fileManager fileExistsAtPath:databasePath];

    if(success) return;

    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"mydb.sqlite"];

    [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];

}

Upvotes: 1

V-Xtreme
V-Xtreme

Reputation: 7333

There are some reason for due to which the sqlite3_prepare_v2 != SQLITE_OK :

  1. The table may not present into the database.

  2. Wrong query statement .

  3. Wrong column name into the query statement.

You can find the exact problem using error statement by putting following in else:

 NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database))

Upvotes: 7

Related Questions