vendredi 9 octobre 2015

Can't retrieve SQLite DB Objective C

I save an image path inside an SQLite DB, when I run the following code in the simulator everything works fine, but when I try it on a real device it doesn't work anymore

This is the code of the InitDB and getImgPath methods:

-(void) initDB{

    NSString *docsDir;
    NSArray *dirPaths;
    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    docsDir = [dirPaths objectAtIndex:0];
    databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"ImgPath.sqlite"]];

    NSFileManager *filemgr = [NSFileManager defaultManager];
    if ([filemgr fileExistsAtPath: databasePath ] == NO)
    {
        const char *dbpath = [databasePath UTF8String];

        if (sqlite3_open(dbpath, &ImgDB) == SQLITE_OK)
        {
            char *errMsg;
            const char *sql_stmt = "CREATE TABLE IF NOT EXISTS ImgPath (autoId INTEGER PRIMARY KEY, path TEXT)";


            if (sqlite3_exec(ImgDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
            {

            }

            sqlite3_close(ImgDB);

        }
    }


}

-(NSString*)getImgPath{

    NSString* imgPath;
    const char *dbpath = [databasePath UTF8String];
    sqlite3_stmt *statement;
    if (sqlite3_open(dbpath, &ImgDB) == SQLITE_OK)
    {
        NSString *querySQL = [NSString stringWithFormat:@"SELECT COUNT(*) FROM ImgPath"];
        NSLog(@"Data = %@",querySQL);

        const char *query_stmt = [querySQL UTF8String];

        sqlite3_prepare_v2(ImgDB, query_stmt, -1, &statement, NULL);
        {
            if (sqlite3_step(statement) == SQLITE_ERROR) {
                NSAssert1(0,@"Errore  %s",sqlite3_errmsg(ImgDB));
            }
            else
            {
                NSLog(@"numero: %d",sqlite3_column_int(statement, 0));
            }
        }
        sqlite3_finalize(statement);
        sqlite3_close(ImgDB);
    }
    return imgPath;
}

When I save the path I NSLog the variables and the paths are correct, but when I NSLog the number of rows I get 0 and I really have no idea why

Aucun commentaire:

Enregistrer un commentaire