2013-05-14 157 views
0

我正在做一個應用程序,它從Web服務獲取JSON數據並在SQLite數據庫中進行更新。我的問題是我從Web服務獲得的價值,但上傳時顯示'空'。它沒有得到更新。Sqlite數據庫不會更新來自Web服務的數據

我的代碼: -

-(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) 
+0

什麼是'sqlExecuteObj'?你爲什麼使用'executeSelectQuery'來執行'UPDATE'? – trojanfoe 2013-05-14 10:36:47

+1

sqlExecute.obj是SQLite類中的一個變量,我在SQLite.h中使用它,executeQuery是執行sqlExecuteobj.sql保存的查詢的方法。 – Logunath 2013-05-14 10:39:27

+1

我使用了相同的過程來存儲任何靜態變量在我的代碼的不同位置,它的工作原理,但聽到它不工作。 – Logunath 2013-05-14 10:40:34

回答

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是用戶驅動的,或者可能只是包含非字母數字字符,則應該(綁定),完整性檢查,轉義這些值。這同樣適用於任何其他值被輸入到數據庫 - 但這不是範圍;-)

+1

嗨,謝謝你的回答。實際上,我通過更改變量名進行了許多代碼檢查,這就是背後的原因。但'selectedProfileNAme'和'ProfileDEtailTitle'都具有相同的值。 – Logunath 2013-05-15 06:32:14