sqlite数据库iOS中的内存问题?

时间:2016-04-06 10:18:56

标签: ios objective-c iphone sqlite storyboard

我在我的应用程序中使用了sqlite数据库。我正在进行CURD操作。我从json获取数据然后我将数据转换为数组,然后从数组中将数据插入到sqlite数据库中。在我的应用程序中数据可以达到超过1000条记录。当数据库完成后,我得到内存警告

我有一个插入数据的功能。首先检查数据是否存在然后更新数据,否则插入新记录

[[DBManager getSharedInstance]insertData:object];

功能定义 -

 (BOOL) insertData:(SubComponent *)subcomponent
{


    const char *dbpath = [databasePath UTF8String];
    if([self getSubComponentDataBySubclientClientInspectionid:subcomponent.componentid : subcomponent.componentsubclientid:subcomponent.componentClientid : subcomponent.componentinspectionid] != nil)
    {

        [self updateCompSubComponentDataBySubclientId:subcomponent];
        return YES;
    }
    else
    {
        if (sqlite3_open(dbpath, &database) == SQLITE_OK)
        {

            if (sqlite3_exec(database, [@"PRAGMA CACHE_SIZE=500000;" UTF8String], NULL, NULL, NULL) == SQLITE_OK) {
                NSLog(@"Successfully changed cache size");
            }
            else
                NSLog(@"Error: failed to set cache size with message %s.", sqlite3_errmsg(database));
            NSLog(@"inside isnert query");
            NSString *insertSQL = [NSString stringWithFormat:
                                   @"insert into subcomponent(subcomponentid,name,createddate,subclientid,clientid,cliententityid, inspectionid, initialstatus,componentclientname,componentsubclientname, checkedstatus) values (\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%ld\",\"%@\",\"%@\",\"2\")",subcomponent.componentid,subcomponent.componentName,subcomponent.componentcreateddate,subcomponent.componentsubclientid,subcomponent.componentClientid, subcomponent.componentEntityid,subcomponent.componentinspectionid,(long)subcomponent.componentStatus,subcomponent.componentClientName,subcomponent.componentSubclientName];
            NSLog(@"%@",insertSQL);
            const char *insert_stmt = [insertSQL UTF8String];
            sqlite3_prepare_v2(database, insert_stmt,-1, &statement, NULL);
            if (sqlite3_step(statement) == SQLITE_DONE)
            {

                sqlite3_finalize(statement);
                return YES;
            }
            else
            {
                NSLog(@"REMASH DB ERROR %s",sqlite3_errmsg(database));
               // sqlite3_reset(statement);
                //sqlite3_close(database);
                sqlite3_finalize(statement);
                return NO;
            }
        }
        sqlite3_close(database);
    }
    return YES;
}

获取数据功能

- (SubComponent*) getSubComponentDataBySubclientClientInspectionid:(NSString*)componentid : (NSString *)subclientid : (NSString *)clientId : (NSString *)inspectionid
{
    const char *dbpath = [databasePath UTF8String];
    if (sqlite3_open(dbpath, &database) == SQLITE_OK)
    {
        NSString *querySQL = [NSString stringWithFormat:@"select * from subcomponent where subcomponentid = \"%@\" and subclientid = \"%@\" and clientid = \"%@\" and inspectionid = \"%@\"",componentid,subclientid, clientId,inspectionid];
        NSLog(@"gghgh query is %@",querySQL);
        const char *query_stmt = [querySQL UTF8String];
        SubComponent *resultArray = [[SubComponent alloc]init];
        if (sqlite3_prepare_v2(database,query_stmt, -1, &statement, NULL) == SQLITE_OK)
        {
            if (sqlite3_step(statement) == SQLITE_ROW)
            {
                NSString *component_id = [[NSString alloc] initWithUTF8String:
                                          (const char *) sqlite3_column_text(statement, 0)];
                resultArray.componentid = component_id;
                NSString *name = [[NSString alloc] initWithUTF8String:
                                  (const char *) sqlite3_column_text(statement, 1)];
                resultArray.componentName = name;
                NSString *createddate = [[NSString alloc] initWithUTF8String:
                                         (const char *) sqlite3_column_text(statement, 2)];
                resultArray.componentcreateddate = createddate;
                NSString *subclientid = [[NSString alloc] initWithUTF8String:
                                         (const char *) sqlite3_column_text(statement, 3)];
                resultArray.componentsubclientid = subclientid;
                NSString *clientid = [[NSString alloc] initWithUTF8String:
                                      (const char *) sqlite3_column_text(statement, 4)];
                resultArray.componentClientid = clientid;
                NSString *entityid = [[NSString alloc] initWithUTF8String:
                                      (const char *) sqlite3_column_text(statement, 5)];
                resultArray.componentEntityid = entityid;
                NSString *inspectionid = [[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 6)];
                resultArray.componentinspectionid = inspectionid;
                NSString *clientName = [[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 8)];
                resultArray.componentClientName = clientName;
                NSString *subclientName = [[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 9)];
                resultArray.componentSubclientName = subclientName;
//                sqlite3_finalize(statement);
//                sqlite3_close(database);
                return resultArray;
            }
            else
            {


//                sqlite3_finalize(statement);
//                sqlite3_close(database);
                return nil;
            }
        }
        else
        {
            NSLog(@"error is %s",sqlite3_errmsg(database));
        }
    }
  //  sqlite3_reset(statement);
   // sqlite3_finalize(statement);
    sqlite3_close(database);

    return nil;
}

更新功能

-(void)updateCompSubComponentDataBySubclientId:(SubComponent *)subcomponent
{
    NSLog(@"inside update data");
    const char *dbpath = [databasePath UTF8String];
    if (sqlite3_open(dbpath, &database) == SQLITE_OK)
    {


            if (sqlite3_exec(database, [@"PRAGMA CACHE_SIZE=500000;" UTF8String], NULL, NULL, NULL) == SQLITE_OK) {
                NSLog(@"Successfully changed cache size");
            }
            else
                NSLog(@"Error: failed to set cache size with message %s.", sqlite3_errmsg(database));



        NSString *querySql=[NSString stringWithFormat:
                            @"UPDATE subcomponent SET name=\"%@\", createddate=\"%@\", cliententityid =\"%@\",componentclientname=\"%@\", componentsubclientname = \"%@\", checkedstatus = \"2\" where subcomponentid = \"%@\" and subclientid = \"%@\" and clientid = \"%@\" and inspectionid =\"%@\"",subcomponent.componentName,subcomponent.componentcreateddate, subcomponent.componentEntityid,subcomponent.componentClientName,subcomponent.componentSubclientName,subcomponent.componentid, subcomponent.componentsubclientid,subcomponent.componentClientid,subcomponent.componentinspectionid];
        const char *sql=[querySql UTF8String];
        if(sqlite3_prepare_v2(database,sql, -1, &statement, NULL) == SQLITE_OK)
        {
            if(SQLITE_DONE != sqlite3_step(statement))
            {
                NSLog(@"REMASH Error while updating. '%s'", sqlite3_errmsg(database));
            }
            else
            {
                //sqlite3_reset(statement);
                NSLog(@"Update done successfully!");
            }
        }

        NSLog(@"NOT OK");
        sqlite3_finalize(statement);
        sqlite3_close(database);
    }


}

PS :有人可以通过查看我的代码来建议是否使用sqlite_finilaize(),sqlite_close()。sqlite_reset()是正确的。因为内存在增加。

1 个答案:

答案 0 :(得分:-2)

这不是数据库问题。

导致此问题是因为应用程序没有为您尝试运行的设备提供故事板。

XCode no valid compiled storyboard at path