sqlite 3开放问题 [英] sqlite 3 opening issue

查看:46
本文介绍了sqlite 3开放问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用sqlite3文件通过几种类似的方法获取数据,如以下代码所示:

I'm getting my data ,with several similar methods, from sqlite3 file like in following code:

-(NSMutableArray *) getCountersByID:(NSString *) championID{

    NSMutableArray *arrayOfCounters;
    arrayOfCounters = [[NSMutableArray alloc] init];

    @try {
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSString *databasePath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"DatabaseCounters.sqlite"];
        BOOL success = [fileManager fileExistsAtPath:databasePath];

        if (!success) {
            NSLog(@"cannot connect to Database! at filepath %@",databasePath);
        }
        else{
            NSLog (@"SUCCESS getCountersByID!!");
        }
        if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK){

            NSString *tempString = [NSString stringWithFormat:@"SELECT COUNTER_ID FROM COUNTERS WHERE CHAMPION_ID = %@",championID];
            const char *sql = [tempString cStringUsingEncoding:NSASCIIStringEncoding];
            sqlite3_stmt *sqlStatement;

            int ret = sqlite3_prepare(database, sql, -1, &sqlStatement, NULL);

            if (ret != SQLITE_OK) {
                NSLog(@"Error calling sqlite3_prepare: %d", ret);
            }
            if(sqlite3_prepare_v2(database, sql, -1, &sqlStatement, NULL) == SQLITE_OK){

                while (sqlite3_step(sqlStatement)==SQLITE_ROW) {
                    counterList *CounterList = [[counterList alloc]init];
                    CounterList.counterID = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,0)];
                    [arrayOfCounters addObject:CounterList];
                }
            }
            else{
                NSLog(@"problem with database prepare");
            }
            sqlite3_finalize(sqlStatement);
        }
        else{
            NSLog(@"problem with database openning %s",sqlite3_errmsg(database));
        }   
    }
    @catch (NSException *exception){
        NSLog(@"An exception occured: %@", [exception reason]);
    }
    @finally{
        sqlite3_close(database);
        return arrayOfCounters;
    }
    //end
}

然后我可以使用此代码和其他类似的代码行访问数据:

then i'm getting access to data with this and other similar lines of code:

myCounterList *MyCounterList = [[myCounterList alloc] init];
countersTempArray = [MyCounterList getCountersByID:"2"];

[countersArray addObject:[NSString stringWithFormat:@"%@",(((counterList *) [countersTempArray objectAtIndex:i]).counterID)]];

我得到了很多数据,例如图像名称,并显示了它们的组合,这取决于用户使用以下代码输入的信息:

I'm getting a lot of data like image name and showing combination of them that depends on users input with such code:

UIImage *tempImage = [UIImage imageNamed:[NSString stringWithFormat:@"%@_0.jpg",[countersArray objectAtIndex:0]]];

[championSelection setBackgroundImage:tempImage forState:UIControlStateNormal];


我的问题:

当我运行我的应用程序一段时间并获取大量数据时,它会引发错误:数据库打开问题无法打开数据库文件-错误= 24(打开的文件太多)"

When i'm run my app for some time and get a lot of data it throws error: " problem with database openning unable to open database file - error = 24 (Too many open files)"

我的猜测是,每次调用getCountersByID时我都会打开我的数据库,但不会关闭它.

My guess is that i'm opening my database every time when getCountersByID is called but not closing it.

我的问题:

我使用正确的方法打开和关闭我使用的数据库吗?

Am i using right approach to open and close database that i use?

不能帮助我解决该问题的类似问题:

Similar questions that did not helped me to solve this problem:

  1. 无法打开数据库
  2. Sqlite打开错误:无法打开数据库


更新:

我以为出现错误是因为我过多地使用了以下代码行:

I made assumption that error is showing up because i use this lines of code too much:

NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *databasePath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"DatabaseCounters.sqlite"];
BOOL success = [fileManager fileExistsAtPath:databasePath];

并以错误24结尾.

所以我将它们设置为全局,但 sqlite3_errmsg 显示了相同的错误24,但应用程序现在运行得更快

So i made them global but sqlite3_errmsg shows same err 24, but app runs much faster now

推荐答案

基本上,在处于 initialization 阶段时,应该只打开一次 DB ,而不是在打开时正在向您的 DB 请求一些信息.但是,您的代码应该不会失败,因为您似乎在每个请求之后都打开了然后关闭了数据库.通过记录这些事件或通过代码调试来确保发生这种情况.

You should open your DB only once basically when you are in the initialization phase but not when you are requesting some info to your DB. Your code shouldn't failed though since you seems to open then close the DB after each request. Make sure this is happening by either logging those events or debugging through your code.

这篇关于sqlite 3开放问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆