0

我有 sql 以及我的应用程序中使用的核心数据。现在,如果我假设进行一些更改,使用 sql 查询更新 sql db,我已经进行了这些更新。如果我发出 fetch 请求并打印数组,我会看到相同的旧值,除非我退出应用程序并重新启动它。我无法理解这种行为。我想知道如果在 sql db 中进行了更改,那么这些更改是否反映在核心数据表中?如果没有,那么我是否必须将更新的值保存在 coredata 中,这会导致重复或冗余数据吗?这是用于更新值的代码:
我有一个包含值的数组:

   - (void) updateGeneraldata3 :(NSArray*) marrDataUpdate 
    {
                                                                                                   sqlite3 *database;
sqlite3_stmt *update_statement = nil;

if(sqlite3_open([strDatabasePath UTF8String], &database) == SQLITE_OK)
{
    NSString *strMQueryupdate = [NSString stringWithFormat:@"UPDATE zform_general_data1 SET zChk_Mild='%@', zChk_CTV='%@', zChk_DOA='%@',           zChk_ALS1='%@' where zautonumber='%@'",[marrDataUpdate objectAtIndex:0],[marrDataUpdate objectAtIndex:1],[marrDataUpdate objectAtIndex:2],[marrDataUpdate objectAtIndex:3],autoNumberTextField.text];

    const char *sql = [strMQueryupdate UTF8String];

    if (sqlite3_prepare_v2(database, sql, -1, &update_statement, NULL) != SQLITE_OK) {
        NSLog(@"update fails");
    }
    else
    {

        sqlite3_bind_text(update_statement, 1, [[marrDataUpdate objectAtIndex:0]  UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(update_statement, 2, [[marrDataUpdate objectAtIndex:1]  UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(update_statement, 3, [[marrDataUpdate objectAtIndex:2]  UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(update_statement, 4, [[marrDataUpdate objectAtIndex:3]  UTF8String], -1, SQLITE_TRANSIENT);           
      int success = sqlite3_step(update_statement);
        sqlite3_reset(update_statement);
        if (success == SQLITE_ERROR){}
        else {}
    }
    sqlite3_finalize(update_statement);
   }
   sqlite3_close(database);   

   }
                                                                   And after update there is a background function which is executed after every 60 seconds of time .Now if i change the value of these fields on the view using the respective text fields, it updates the value in db using the above code which is also being executed after a certain interval of time  :                                                                                        


 -(void) functServerPostingData  

 {

 @try 
  {
    @autoreleasepool {


    generalDataObject = [[GeneralData alloc] init];
    generalData1_Array=[[NSArray alloc]init];
           [self checkUsedAutoNumber];
    NSError *error;
    NSManagedObjectContext *mocGeneral1 = [self managedObjectContext];
    NSEntityDescription *entityDescriptionGeneral1 = [NSEntityDescription
                                                      entityForName:@"Form_General_Data1" inManagedObjectContext:mocGeneral1];
    NSFetchRequest *requestGeneral1;

    requestGeneral1 = [[NSFetchRequest alloc] init];

    [requestGeneral1 setEntity:entityDescriptionGeneral1];

    generalData1_Array=[mocGeneral1 executeFetchRequest:requestGeneral1 error:&error];
     NSLog(@"generalData1_Array :%@",generalData1_Array);
                if(!generalData1_Array)
    {

        NSLog(@"Error fetching Login: %@", [error localizedDescription]);
        abort();

    }


    for(int i = 0; i< [generalData1_Array count]; i++)  {
        for (int j=0;j<[[strAutoNumber componentsSeparatedByString:@","] count] ; j++)
        {

            if([[[generalData1_Array objectAtIndex:i] Autonumber ] isEqualToString: [[strAutoNumber componentsSeparatedByString:@","]objectAtIndex:j]])
            {
                generalDataObject.AutoNumber=[NSString stringWithFormat:@"%@",[[generalData1_Array objectAtIndex:i] Autonumber ]]; 

                generalDataObject.Chk_Mild =[[generalData1_Array objectAtIndex:i]Chk_Mild];

                generalDataObject.Chk_CTV=[[generalData1_Array objectAtIndex:i]Chk_CTV];


                generalDataObject.Chk_PAU=[[generalData1_Array objectAtIndex:i]Chk_PAU];
                                   generalDataObject.Transient=[[generalData1_Array objectAtIndex:i]Transient];

              generalDataObject.Chk_ALS1=  [[generalData1_Array objectAtIndex:i]Chk_ALS1];


              [generalDataObject startPostServerData];
            }
        }
    }

    if (generalDataObject.checkRequest==TRUE) 
    {
        [appDelegate CheckInternetConnection];
        if(appDelegate.internetWorking==-1)
        {
        }
        else {
            serverLinkToHit = [NSString stringWithFormat:@"%@/ReceiveData.aspx?m=InsertData&t=GeneralData",[serverLinkSettings getServerLink ]];
            [generalDataObject readServerData:serverLinkToHit postServerData:generalDataObject.finalDataString];
        }

    }
    generalDataObject = nil;
        requestGeneral1=nil;
        requestGeneral2=nil;
    }
    } 
@catch (NSException *exception)
  {
    [ExceptionLog LogEntry:exception :@"Error while entering data functServerPostingData"];
 }


 }

我打印了始终显示以前的旧值的数组,无论我在文本字段中输入了什么并保存在数据库中。但是,在同一个地方我打印它们更新的 sql db 的值。我应该使用 sql select 来选择值把它放在一个数组中然后发布?

4

1 回答 1

1

不,这不是 Core Data 的工作方式。数据库的格式没有记录,你不应该玩弄它。

我认为这里真正的问题是:为什么你认为你需要使用 SQL?看起来您只是在更新一些记录。使用“本机”核心数据非常简单。

于 2012-10-01T10:41:08.457 回答