iOS错误尝试附加2个SQLite数据库

时间:2012-06-19 10:08:46

标签: ios database xcode sqlite

我是Ios开发人员,我在附加2个SQLite数据库时遇到问题。 我们在运行时收到此错误:

Error calling sqlite3_step (1: SQL logic error or missing database) SQLITE_ERROR
2012-06-19 11:10:19.658 °°°°°°°[453:707] DB Query: ATTACH DATABASE '/var/mobile/Applications/A05034A8-F5AF-45AA-B520-DF2BBEBB800B/Documents/Meals.db' AS mealDB
2012-06-19 11:10:19.672 °°°°°°°[453:707] Err 1: no such table: Meal
2012-06-19 11:10:19.680 °°°°°°°[453:707] Error calling sqlite3_step (1: SQL logic error or missing database) SQLITE_ERROR
2012-06-19 11:10:19.684 °°°°°°°[453:707] DB Query: ATTACH DATABASE '/var/mobile/Applications/A05034A8-F5AF-45AA-B520-DF2BBEBB800B/Documents/Meals.db' AS mealDB
2012-06-19 11:10:19.693 °°°°°°°[453:707] Err 1: no such table: MealItem
2012-06-19 11:10:19.698 °°°°°°°[453:707] ERREUR

附加后,我们正在尝试收集数据。他找不到我们正在搜索的表:错误1:没有这样的表:MealItem或Err 1:没有这样的表:用餐。

它变得更加奇怪! 当我们使用iOS 5.0在Sim中测试相同的代码时,它可以工作! 数据从请求返回! 尽管如此仍然会出现这种错误:

Error calling sqlite3_step (1: SQL logic error or missing database) SQLITE_ERROR
2012-06-19 11:10:19.684 °°°°°°°[453:707] DB Query: ATTACH DATABASE '/var/mobile/Applications/A05034A8-F5AF-45AA-B520-DF2BBEBB800B/Documents/Meals.db' AS mealDB

我们尝试在sqlight管理器中附加2个数据库,这很成功。 此结果数据库也在应用程序中工作。 似乎iOS附加数据库有问题。 或者我们在某处犯了错误。

到目前为止,我们正在寻找一个没有任何运气的解决方案。

这里有一些代码: (使用FMDatabase api)

- (FMDatabase *) attachMealDatabase:(FMDatabase *) db {


    if (![db open]) {
        NSLog(@"Could not open db.");
        return nil;
    }else {

    }

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDirectory = [paths objectAtIndex:0];
    //const char * mainDBPath = [[documentDirectory stringByAppendingPathComponent:@"Main.db"] UTF8String];
    const char * mealDBPath = [[documentDirectory stringByAppendingPathComponent:@"Meals.db"] UTF8String];



    NSString *attachSQL = [NSString stringWithFormat: @"ATTACH DATABASE \'%s\' AS mealDB", mealDBPath];


    [db beginTransaction];

    [db executeUpdate:attachSQL];
    [db commit];
    //[db close];

    return db;

}

1 个答案:

答案 0 :(得分:4)

尝试在FMDatabase图层而不是viewcontrollers中附加数据库。 我们通过调用以下函数在Db open函数中完成它:

-(void)attachDb{

  NSArray paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  NSString documentDirectory = [paths objectAtIndex:0];
  const char  secondDBPath = [[documentDirectory    stringByAppendingPathComponent:@"Second.db"] UTF8String];

  if (sqlite3_open([databasePath UTF8String], &db) == SQLITE_OK)
  {
    NSString *strSQLAttach = [NSString stringWithFormat:@"ATTACH DATABASE \'%s\' AS SECOND", secondDBPath ];
    char *errorMessage;

    if (sqlite3_exec(db, [strSQLAttach UTF8String], NULL, NULL, &errorMessage) == SQLITE_OK)
    {
        DLog(@"Great Success!");
    }
  }    
}

希望这适合你!