1

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

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

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

它变得更加陌生!当我们使用 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 manager 中附加 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;

}
4

1 回答 1

4

尝试将数据库附加在FMDatabase图层中而不是视图控制器中。我们通过调用以下函数在 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!");
    }
  }    
}

希望这对你有用!

于 2012-06-19T13:09:00.920 回答