Cocoa sqlite db文件

时间:2012-10-17 20:14:55

标签: objective-c cocoa sqlite

我在理解以下代码无效的原因时遇到了一些麻烦:

sqlite3_stmt *statement5;

NSString *databasePath2 = @"default-gameData.db";
NSInteger speed_int = 0;

const char *dbPath2 = [databasePath2 UTF8String];

if(sqlite3_open(dbPath2, &db)==SQLITE_OK){
    NSString *getspeeds = @"SELECT * FROM planeInfo";
    NSLog(@"%@",getspeeds);
    const char *getspeed_stmt = [getspeeds UTF8String];

    if(sqlite3_prepare_v2(db, getspeed_stmt, -1, &statement5, nil)==SQLITE_OK){

        NSLog(@"%d",sqlite3_step(statement5));

        NSString *org_plane_info_id = [[NSString alloc] init];

        while(sqlite3_step(statement5) == SQLITE_ROW){
            if((const char*)sqlite3_column_text(statement5, 1)==NULL){
                org_plane_info_id = @"no";
            }
            else{
                org_plane_info_id = [[NSString alloc] initWithUTF8String:(const char*)sqlite3_column_text(statement5, 1)];
            }
            NSLog(@"%@",org_plane_info_id);
            if(org_plane_info_id==plane_info_id){
                NSString *speed = [[NSString alloc] initWithUTF8String:(const char*)sqlite3_column_text(statement5, 2)];
                NSLog(@"%@",speed);
                speed_int = [speed integerValue];

                return speed_int;
            }
        }
    }
}
if(speed_int==0){
    return 0;
}
sqlite3_close(db);

我发现问题的根源是数据库路径,文件存储在.h和.m文件的位置,所以我认为这是我出错的地方,但是我需要做些什么来实现工作?

1 个答案:

答案 0 :(得分:2)

Cocoa / Foundation不知道你的文件在哪里。你需要告诉它它在应用程序包中。

NSString *databasePath2 = [[NSBundle mainBundle] pathForResource:@"default-gameData" ofType:@"db"];