Sqlite DB不存在这样的表



我在iPhone模拟器文档中有一个数据库。我现在确定它在应用程序沙箱中。我的代码有点不对劲。所以我首先得到DB路径:

-(NSString *)getsynDbPath
{
    NSString* dataBAse = [[NSBundle mainBundle] pathForResource:@"ddd"ofType:@"sqlite"];
    return dataBAse;
}

然后测试路径:

    NSString *testData;
    testData = [self getsynDbPath];
    NSFileManager * fileManager = [NSFileManager defaultManager];
    BOOL success = [fileManager fileExistsAtPath:testData];
    if (success) {
        NSLog(@"Oh no! There was a big problem!");
    } else {
       //Successfully opened 
        if(sqlite3_open([testData UTF8String], &db)==SQLITE_OK){
            NSLog(@"Raise the roof!");
            //Calling method to loop through columns
            [self listOfCols];
        }

    }

然后转到一个自定义方法,循环遍历数据库中的列:

-(NSArray *)listOfCols{
    NSMutableArray *retval = [[[NSMutableArray alloc]init]autorelease];
    NSString *query = @"SELECT KEY_ID FROM CON_DETAIL";
    sqlite3_stmt *statement;
      //Does not execute
    if (sqlite3_prepare_v2(db, [query UTF8String], -1, &statement, nil)==SQLITE_OK) {
        while (sqlite3_step(statement)==SQLITE_ROW) {
            int key_id = sqlite3_column_int(statement, 0);
            NSLog(@"Key ID: %d", key_id);
            char *nameChars = (char *) sqlite3_column_text(statement, 1);
            NSLog(@"chars %s", nameChars);
            char *cityChars = (char *) sqlite3_column_text(statement, 2);
            NSLog(@"chars %s", cityChars);
       }
    }
    NSLog(@"%s Why '%s' (%1d)", __FUNCTION__, sqlite3_errmsg(db), sqlite3_errcode(db));
    return retval;
}

我的问题是。在我成功打开数据库后,为什么我得到一个日志错误,说:no such table: CON_DETAIL ?任何帮助都是感激的。

我认为你必须复制你的db在你的文档目录,然后尝试获取。用下面的函数复制它。

-(void) dbconnect{
    self.databaseName = @”yourdbname.sqlite”;
    // Get the path to the documents directory and append the databaseName
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [documentPaths objectAtIndex:0];
    self.databasePath = [documentsDir stringByAppendingPathComponent:self.databaseName];
    // Execute the “checkAndCreateDatabase” function
    [self checkAndCreateDatabase];
}
-(void) checkAndCreateDatabase{
    // Check if the SQL database has already been saved to the users phone, if not then copy it over
    BOOL success;
    // Create a FileManager object, we will use this to check the status
    // of the database and to copy it over if required
    NSFileManager *fileManager = [NSFileManager defaultManager];
    // Check if the database has already been created in the users filesystem
    success = [fileManager fileExistsAtPath:databasePath];
    // If the database already exists then return without doing anything
    if(success) {
        return;
    }
    // If not then proceed to copy the database from the application to the users filesystem
    // Get the path to the database in the application package
    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
    // Copy the database from the package to the users filesystem
    [fileManager copyItemAtPath:databasePathFromApp toPath:self.databasePath error:nil];
    [fileManager release];
}

注意:如果你的应用程序的文档目录中没有得到db,请执行以下操作。转到:Target ->"Build Phases"->"copy bundle Resources",然后在这里添加特定的文件。

之后调用你的"listOfCols"方法

相关内容

最新更新