【问题标题】:How to open sqlite database on ios 6?如何在 ios 6 上打开 sqlite 数据库?
【发布时间】:2012-11-27 00:34:21
【问题描述】:

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

~/Library/Application Support/iphone模拟器/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;

}

如果您知道我犯了什么错误,请告诉我。

谢谢。

【问题讨论】:

  • 旁注:NSString *rawquery = @"select * from people"; const char *query = [rawquery UTF8String]; 是非常多余的,为什么不只是const char *query = "select * from people";?另外,您确定数据库中确实有一个名为“People”的表吗?
  • 当我声明为 const char * 时,编译器显示错误:“ARC 不允许将 Objective-C 指针隐式转换为 'const char *'”。表名正确。
  • 不足以更改声明,同时省略@
  • 我在哪里省略了@? XCode 没有显示此错误。而且我不认为我省略了@。
  • 这就是问题所在。 请省略 @

标签: ios sqlite ios-simulator


【解决方案1】:

我自己解决了这个问题。当我调试应用程序时,我看到它没有调用applicationDidFinishLaunching 方法,它调用了applicationDidFinishLaunchingWithOptions 方法。我只是将代码放在applicationDidFinishLaunchingWithOptions 方法中调用copyDatabaseToDocument 并且它可以工作。 这是我的 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() 处启动。如果你知道,请给我一个描述。

谢谢。

【讨论】:

  • applicationDidFinishLaunchingWithOptions 自 iOS 3.0 以来一直是首选方式
猜你喜欢
  • 1970-01-01
  • 2011-06-10
  • 1970-01-01
  • 1970-01-01
  • 2016-03-02
  • 2018-11-22
  • 1970-01-01
  • 1970-01-01
  • 2012-03-23
相关资源
最近更新 更多