可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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 :
The table may not present into the database.
Wrong query statement .
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]; }