如何在ios 6上打开sqlite数据库?

时间:2012-12-09 10:31:32

标签: ios sqlite ios-simulator

我是ios开发的新手。我现在尝试做的是打开现有的sqlite数据库并从那里选择数据。我调试我的源代码并看到我打开数据库成功(我认为我成功,因为*数据库指针不是nil)。但是当我使用sqlite3_prepare_v2()来初始化select查询时,我总是收到错误:“没有这样的表People”。我在路径上查了一下:

  

〜/ Library / Application Support / iphone simulator / 6.0 / Application //.

数据库被成功复制,我可以打开它看看那里的数据。

这是我复制数据库并打开它的代码:

- (NSString*) getDatabasePath{
    //Search for standard documents using NSSearchPathForDirectoriesInDomains
    //First Param = Searching the documents directory
    //Second Param = Searching the Users directory and not the System
    //Expand any tildes and identify home directories.
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];
    return [documentsDir stringByAppendingPathComponent:@"data.sqlite"];
}

- (void)copyDatabaseToDocument {
    //Using NSFileManager we can perform many file system operations.
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSString *dbPath = [self getDatabasePath];
    BOOL success = [fileManager fileExistsAtPath:dbPath];

    if(!success) {

        NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"data.sqlite"];
        success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];

        if (!success)
            NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
    }
}

- (sqlite3*)openDatabaseConnection {
    sqlite3 *database;
    NSString * path = [self getDatabasePath];

    if (sqlite3_open([path UTF8String], &database) != SQLITE_OK) {
        sqlite3_close(database);
        NSAssert1(0, @"Failed to open database with message '%s'.", sqlite3_errmsg(database));
    }
    return database;
}

这是我选择数据的代码。错误发生在以下行:sqlite3_prepare_v2(database, query, -1, &selectStatement, NULL)

- (People*) getPeople{
    sqlite3 *database = [[[DBConnector alloc] init] openDatabaseConnection];
    if(database == nil)
        return nil;
    sqlite3_stmt *selectStatement;
    NSString *rawquery      = @"select * from people";
    const char *query       = [rawquery UTF8String];
    NSMutableArray* result = [[NSMutableArray alloc] init];

    if (sqlite3_prepare_v2(database, query, -1, &selectStatement, NULL) == SQLITE_OK) {
        while (sqlite3_step(selectStatement) == SQLITE_ROW) {

            //Parse the data by calling a private method:
            People *people = [self parsePeopleWithStatement:selectStatement];

            [result addObject:people];
        }
    }else{
        NSAssert1(0, @"Error: '%s'.", sqlite3_errmsg(database));
    }
    sqlite3_finalize(selectStatement);
    return result;

}

如果你知道我的错误,请告诉我。

感谢。

1 个答案:

答案 0 :(得分:1)

我自己解决了这个问题。当我调试应用时,我发现它没有调用applicationDidFinishLaunching方法,它调用了applicationDidFinishLaunchingWithOptions方法。我只是将代码放在copyDatabaseToDocument方法中调用applicationDidFinishLaunchingWithOptions,然后就可以了。 这是我的Delegate类的代码:

//THIS METHOD WAS NOT CALLED
- (void)applicationDidFinishLaunching:(UIApplication *)application{
    DBConnector *connector = [[DBConnector alloc] init];
    [connector copyDatabaseToDocument];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
       self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

       //SHOW MY SCREEN
        (...)

       self.window.backgroundColor = [UIColor whiteColor];
       [self.window makeKeyAndVisible];
       return YES;
}

顺便说一句,我认为这不是一个真正的答案同步我不知道为什么我的应用程序没有从applicationDidFinishLaunching()开始。如果您知道,请给我一个描述。

感谢。