不知道为什么 sqlite3_prepare_v2 等于 false。我在网上搜索并找不到太多,我找到的也没有帮助。一个站点建议使用 sqlite3_errmsg(database) 并且由于某种原因输出“不是错误”。另一个在线答案建议删除 iPhone 模拟器中以十六进制字符串命名的文件夹,但这也不起作用。我创建了我的数据库并放入了支持文件文件夹,所以它在那里并且有记录。
这是我的代码:
-(void)readMovesFromDatabaseWithPath:(NSString *)filePath
{
sqlite3 *database;
printf("Here in readMovesFromDatabaseWithPath\n");
if(sqlite3_open([filePath UTF8String], &database) == SQLITE_OK)
{
NSLog(@"Now in readMovesFromDatabaseWithPath\n");
const char *sqlStatement = "select * from moves";
sqlite3_stmt *compiledStatment;
printf( "could not prepare statemnt: %s\n", sqlite3_errmsg(database) ); //returns "not an error"
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatment, NULL) == SQLITE_OK)
{
NSLog(@"In sqlite3_prepare_v2 block\n"); //does not reach this line
while(sqlite3_step(compiledStatment) == SQLITE_ROW) //Loops through the database
{
//Extracts the move's name
NSString *moveName = [NSString stringWithUTF8String:(char *) sqlite3_column_text(compiledStatment, 1)];
//Extracts the move's description
NSString *moveDescription = [NSString stringWithUTF8String:(char *) sqlite3_column_text(compiledStatment, 2)];
//Creates new move objects
Moves *newMove = [[Moves alloc] init];
newMove.moveName = moveName;
newMove.moveDescription = moveDescription;
[self.moves addObject:newMove];
}
}
sqlite3_finalize(compiledStatment);
}
sqlite3_close(database);
}