iphone sqlite'无法打开数据库文件'

时间:2010-12-13 06:31:43

标签: iphone sqlite

我正在使用数据库类来使用sqlite数据库

#import "DatabaseConnection.h"

@implementation DatabaseConnection
-(void)DBInitalize{
    databaseName = @"sensorystimulation.sql";
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [documentPaths objectAtIndex:0];
    databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
    [self checkAndCreateDatabase];
    [self readFromDatabase];
}
-(NSMutableArray *)settingsData{
    return settingsArray;
}

-(void)checkAndCreateDatabase{
    BOOL success;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    success = [fileManager fileExistsAtPath:databasePath];
    if(success)
        return;

    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
    [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
}
-(void)readFromDatabase{
    settingsArray = [[NSMutableArray alloc] init];
    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
        const char *sqlStatementNew = "my sql query";
        sqlite3_stmt *compiledStatementNew;
        if(sqlite3_prepare_v2(database, sqlStatementNew, -1, &compiledStatementNew, NULL) == SQLITE_OK) {
            while(sqlite3_step(compiledStatementNew) == SQLITE_ROW) {
                NSString *key_name = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatementNew, 0)];
                NSString *key_value = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatementNew, 1)];
                NSMutableDictionary *tempDic = [[NSMutableDictionary alloc] initWithObjectsAndKeys:key_name,@"key_name",key_value,@"key_value",nil];
                [settingsArray addObject:tempDic];
                [tempDic release];
            }
            sqlite3_finalize(compiledStatementNew);
        }
    }
}
-(void)updateSettings:(NSMutableArray *)values{
    for (int l=0; l<[values count]; l++) {
        NSString *key_name = [[values objectAtIndex:l] objectForKey:@"key_name"];
        NSString *key_value = [[values objectAtIndex:l] objectForKey:@"key_value"];
        sqlite3_stmt *updateStmt;
        NSString *ts=[NSString stringWithFormat:@"UPDATE table key_value='%@' where key_name='%@'",key_value,key_name];
        const char *sql = [ts cStringUsingEncoding:1];
        if(sqlite3_prepare_v2(database, sql, -1, &updateStmt, NULL) != SQLITE_OK){
            NSLog(@"Error while creating update statement. '%s'", sqlite3_errmsg(database));
        }
        if(SQLITE_DONE != sqlite3_step(updateStmt)){
            NSLog(@"%@",ts);
            NSLog(@"Error while updating. '%s'", sqlite3_errmsg(database));
        }
    }
}

-(void)quitApp{
    sqlite3_close(database);
}
@end

并像这样调用它的对象

初始化

databaseConnection = [[DatabaseConnection alloc] init];
[databaseConnection DBInitalize];

更新数据库

NSMutableArray *valueArray = [[NSMutableArray alloc] init];
[valueArray addObject:[[[NSMutableDictionary alloc] initWithObjectsAndKeys:@"abc",@"key_name",[NSString stringWithFormat:@"%d",abc],@"key_value",nil] autorelease]];
[valueArray addObject:[[[NSMutableDictionary alloc] initWithObjectsAndKeys:@"xyz",@"key_name",[NSString stringWithFormat:@"%d",xyz],@"key_value",nil] autorelease]];
[databaseConnection updateSettings:valueArray];
[valueArray release];

工作正常。没有问题是使用

但经过大量更新后大约100-200次以下日志(错误)来了......并且每次发生此错误后我都无法更新数据库。在此之后我必须退出应用程序,然后我再次工作正常

Error while updating. 'unable to open database file'

并且由于我的其他功能,在图像视图上的选项卡出现错误后也无法正常工作

任何想法都要考虑到这一点。请帮忙。

-Amit Battan

1 个答案:

答案 0 :(得分:0)

1)您是否看过这两个问题:
unable to open database
Sqlite Opening Error : Unable to open database

您可能会看到上述问题中报告的类似问题 您似乎在“updateSettings”中缺少sqlite3_finalize 你每次发布“databaseConnection”时都在关闭数据库吗?

2)
你打开DB 100-200倍吗? 如果是这样,也许您应该考虑这是否是您的应用程序的最佳方法。

您是否考虑过使用Singleton进行数据库访问? - &GT;在init期间打开DB一次,然后每次重新使用连接。这可能不仅可以加快您的应用程序速度,还可以减少内存使用量。退房:
http://cocoawithlove.com/2008/11/singletons-appdelegates-and-top-level.html
在您做出决定时,您应该考虑在您的App中多个线程是否同时访问数据库。

最佳,
拉尔夫