我有以下代码(来自此tutorial的修改代码):
-(NSMutableArray *) getChampionDatabase
{
NSMutableArray *championsArray = [[NSMutableArray alloc] init];
@try {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *databasePath = [[NSBundle mainBundle]pathForResource: @"Champions Database" ofType:@"sqlite"];
BOOL success = [fileManager fileExistsAtPath:databasePath];
if (!success)
{
NSLog(@"cannot connect to Database! at filepath %@",databasePath);
}
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
{
const char *sql = "SELECT CHAMPION_ID,CHAMPION_NAME,CHAMPION_IMG FROM CHAMPIONS";
sqlite3_stmt *sqlStatement;
if(sqlite3_prepare(database, sql, -1, &sqlStatement, NULL) == SQLITE_OK)
{
while (sqlite3_step(sqlStatement)==SQLITE_ROW) {
championList *ChampionList = [[championList alloc]init];
ChampionList.championId = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,0)];
ChampionList.championName = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,1)];
ChampionList.championImage = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 2)];
[championsArray addObject:ChampionList];
}
}
else
{
NSLog(@"problem with database prepare");
}
}
else
{
NSLog(@"problem with database openning");
}
}
@catch (NSException *exception)
{
NSLog(@"An exception occured: %@", [exception reason]);
}
@finally
{
return championsArray;
}
}
运行此代码后,我总是输出:“数据库准备问题”
然后我尝试通过以下代码检查sqlite3_prepare的错误号:
int ret = sqlite3_prepare(database, sql, -1, &sqlStatement, NULL);
if (ret != SQLITE_OK) {
NSLog(@"Error calling sqlite3_prepare: %d", ret);
}
输出:“调用sqlite3_prepare时出错:26”
错误26 - / *打开的文件不是数据库文件* /
文件有sqlite 3版本
这怎么可能?文件扩展名是.sqlite,我可以在sqlite manager
中修改它答案 0 :(得分:2)
确保将数据库添加到项目的ressource bundle
,并且不要在数据库名称中添加任何空格。例如,使用champion_db.sqlite
等名称。