简体   繁体   English

iPhone中的SQLite

[英]SQLite in iPhone

I have an app which calls web services and persists the results to an sqlite db, the idea being that when the device is offline, these persisted results are retrieved. 我有一个应用程序,该应用程序调用Web服务并将结果持久保存到sqlite数据库,其想法是,当设备离线时,将检索这些持久结果。 This all works ok, but if I rebuild the project, the database seems to be wiped and I can't get any data out which I previously put in. Can anyone explain what is going on here please? 一切正常,但是如果我重建项目,数据库似乎被擦除了,我无法获取以前放入的任何数据。有人可以解释一下这里发生了什么吗? I am using the 4.3 simulator and Xcode 4. 我正在使用4.3模拟器和Xcode 4。

I am also using the FMDatabase library... 我也在使用FMDatabase库...

-(NSMutableArray*)retrieveDataSourceOfType:(NSString*)type{
    FMDatabase *db = [self getDB];
    NSMutableArray *items = [[NSMutableArray alloc] initWithObjects:nil];
    if([type isEqualToString:@"AlertItem"]){
        FMResultSet *results = [db executeQuery:@"SELECT * FROM AlertItem"];
        while([results next]){
            AlertItem *tmp = [[AlertItem alloc] init];
            tmp.Description = [results stringForColumn:@"Description"];
            tmp.NumItems = [results intForColumn:@"NumItems"];
            tmp.ModuleGroup = [results stringForColumn:@"ModuleGroup"];
            tmp.StaffID = [results intForColumn:@"StaffID"];
            tmp.Status = [results intForColumn:@"Status"];
            [items addObject:tmp];
        }
    }
    [db close];
    [db release];
    return items;
}



-(FMDatabase*)getDB{

NSString *path = [[[NSBundle mainBundle] resourcePath] 
stringByAppendingString:@"/XXXX.sqlite"];
    NSLog(@"%@", path);
    NSFileManager *fm = [NSFileManager defaultManager];
    if([fm fileExistsAtPath:path]){
        FMDatabase *db = [[FMDatabase databaseWithPath:path]retain];
        NSLog(@"%@", path);
        if(![db open]){
            [db release];
            return nil;
        }
        else{
            return db;
        }
    }

}

Thanks. 谢谢。

I think this depends where you put your database file. 我认为这取决于您将数据库文件放在何处。 If it is a resource I think it will be overwitten every time you deploy your project. 如果这是一种资源,我认为每次部署项目时都会被忽略。 You should copy/create your database in the application cache folder. 您应该在应用程序缓存文件夹中复制/创建数据库。

Here is how to get the path: 这是获取路径的方法:

  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); 
  NSString *cacheDirectory = [paths objectAtIndex:0]; 

Here are other directories you could use. 是您可以使用的其他目录。

I would try NSFileManager for file copying. 我会尝试使用NSFileManager进行文件复制。 I'm not sure about these I only use sqlite for autocomplete, no insert/update. 我不确定这些我只使用sqlite自动完成,没有插入/更新。

Try fileExistsAtPath to see fi the file exists and if not you should copy over the empty one from your resources using copyItemAtPath 尝试使用fileExistsAtPath查看文件是否存在,如果不存在,则应使用copyItemAtPath从资源中复制空copyItemAtPath

If you test only on simulator, maybe you make sometimes "Clean All Targets". 如果仅在模拟器上进行测试,则有时可能会“清除所有目标”。 If this is not that case, than pleas show how you open database connection? 如果不是这种情况,那么请显示您如何打开数据库连接? Maybe you open temporary database and it will be removed as soon as connection is closed. 也许您打开了临时数据库,并且在关闭连接后它将被删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM