0

我正在做一个从 Web 服务获取 JSON 数据并在 SQLite 数据库中更新它的应用程序。我的问题是我从 Web 服务中获取了值,但是在上传时显示为“null”。它没有得到更新。

我的代码:-

-(void) updateSignupTable: (NSDictionary*) json {
    //copying the values from web services

    NSString *balance =  [json objectForKey:@"balance"];
    NSString *phone_number =[json objectForKey:@"sim_number"];
    NSString *call_forward_Status = [json objectForKey:@"call_forward_status"];

//将其存储在数据库中

    sqlExecutObj.sql=[NSString stringWithFormat:@"UPDATE profile_table SET phone_number ='%@' balance = '%@' call_forward_status = '%@' WHERE profile_name= '%@'", phone_number ,balance, call_forward_Status, profileDetailTitle];

    sqlExecutObj.dataTypeArray=[[NSMutableArray alloc]initWithObjects:@"1",@"1",@"1",nil];

    [sqlExecutObj executeSelectQuery];
///reloading the table    
    [profileDetailView.detailsTableView reloadData];

    // fetch and print the  updated datas from SQLite

    sqlExecutObj.sql=[NSString stringWithFormat:@"SELECT call_forward_status,phone_number,balance FROM profile_table WHERE profile_name= '%@'",selectedProfileName];

    sqlExecutObj.dataTypeArray=[[NSMutableArray alloc]initWithObjects:@"1",@"1",@"1",nil];

    [sqlExecutObj executeSelectQuery];

    NSLog(@"result from table are: %@. and  %@, and %@", [[sqlExecutObj.result objectAtIndex:0] objectAtIndex:0],[[sqlExecutObj.result objectAtIndex:1] objectAtIndex:0],[[sqlExecutObj.result objectAtIndex:2]objectAtIndex:0] );
}

我的调试输出:-

2013-05-14 15:54:02.910 Movirtu[44262:19d03] result from table are: (null). and  (null), and (null)
4

1 回答 1

0

只是要小心,以下行:

 sqlExecutObj.sql=[NSString stringWithFormat:@"UPDATE profile_table SET phone_number ='%@' balance = '%@' call_forward_status = '%@' WHERE profile_name= '%@'", phone_number ,balance, call_forward_Status, profileDetailTitle];

将 'profileDetailTitle 指定为 profile_name,但在第二个语句中,您正在从数据库中读取数据:

sqlExecutObj.sql=[NSString stringWithFormat:@"SELECT call_forward_status,phone_number,balance FROM profile_table WHERE profile_name= '%@'",selectedProfileName];

指定 profile_name 是“selectedProfileName”。

在您的代码中,profileDetailTitle 或 selectedProfileName 是什么并不明显。也许为了调试,您将它们都设置为相同的值。

另外,为了确定,如果 profileDetailTitle 或 selectedProfileName 是用户驱动的,或者可能只包含非字母数字字符,您应该(或者)绑定、完整性检查、转义这些值。输入到数据库中的任何其他值也是如此 - 但这不是范围;-)

于 2013-05-14T14:06:55.660 回答