sqlite3_prepare_v2 != SQLITE_OK

匿名 (未验证) 提交于 2019-12-03 02:29:01

问题:

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; } 

回答1:

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)) 


回答2:

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];  } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!