2011-08-03 112 views
0

我已經使用dbAdapter編寫了更新表。Android更新sqlite表

public void loadDownloadData() { 
    SoapPrimitive responsePrimitiveData; 
    //Loop Table list 
    for (int i = 0; i < tablesName.size(); i++) { 
     try { 
      responsePrimitiveData = soapPrimitiveData(tablesName.get(i)); 
      if (responsePrimitiveData != null) { 
       try { 
        String result = responsePrimitiveData.toString(); 
        JSONObject jsonobject = new JSONObject(result); 
        JSONArray array = jsonobject.getJSONArray("Table1"); 
        int max = array.length(); 

        // Loop each table data 
        for (int j = 0; j < max; j++) { 
         JSONObject obj = array.getJSONObject(j); 
         JSONArray names = obj.names(); 
         StringBuilder strFields = new StringBuilder(); 
         StringBuilder strValues = new StringBuilder(); 
         String[] strToFields = new String[names.length()]; 
         String[] strToFieldsVal = new String[names.length()]; 

         //getting the Json name, values in separate string array 
         for (int k = 0; k < names.length(); k++) { 
          String name = names.getString(k); 
          strToFields[k] = names.getString(k); 
          String strVal; 
          if(obj.getString(name)== null){ 
           strVal=""; 
           strToFieldsVal[k]=""; 
          }else{ 
           if(obj.getString(name).equals(" ")){ 
            strVal=""; 
            strToFieldsVal[k]=""; 
           }else{ 
            String tmp1 = obj.getString(name).replaceAll("\\s+", " "); 
            String tmp = tmp1.replaceAll("\\s+", " "); 
            strVal =tmp.replaceAll("\\s+", " ");  
            strToFieldsVal[k]=strVal; 
           } 
          } 
          strFields.append(name + ","); 
          strValues.append(strVal+","); 
         } //end of json for loop 

         strFields.deleteCharAt(strFields.length() - 1); 
         strValues.deleteCharAt(strValues.length() - 1); 

         if(getTableUpdateType(tablesName.get(i)).equals("1")){ 
          String actualtable = getAndroidTablename(tablesName.get(i)); 
          if(isTableRecords(tablesName.get(i))){ 
           String[] strWhereField = getTablePrimaryKey(tablesName.get(i),strBusinessUnit); 

           String[] strWhereFieldVal = new String[strWhereField.length]; 
           StringBuilder whereFields = new StringBuilder(); 
           for (int a = 0; a < strWhereField.length; a++) { 
            strWhereFieldVal[a] = obj.getString(strWhereField[a]); 
            whereFields.append(strWhereField[a] + "= ? and "); 
           } 
           whereFields.delete(whereFields.length() - 4, whereFields.length()); 
           updateTableRecords(actualtable, strToFields, strToFieldsVal,whereFields.toString() ,strWhereFieldVal); 
          }else{ 
           insertTableRecords(actualtable, strToFields, strToFieldsVal); 
          } 
         }else if(getTableUpdateType(tablesName.get(i)).equals("2")){ 

         }else if(getTableUpdateType(tablesName.get(i)).equals("3")){ 

         }else{ 

         } 
        }//end of each table data 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
       } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } catch (XmlPullParserException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

,我叫喜歡更新方法:

public void updateTableRecords(String strTableName, String[] strToFields, String[] strValues,String strWhereField ,String[] strWhereFieldVal){ 
    DBAdapter dbAdapter = DBAdapter.getDBAdapterInstance(DownlaodTableActivity.this); 
    dbAdapter.openDataBase(); 

    ContentValues initialValues = new ContentValues(); 
    for(int i=0 ;i<strToFields.length;i++){ 
     initialValues.put(strToFields[i],strValues[i]); 
    } 

    long n = dbAdapter.updateRecordsInDB(strTableName, initialValues, strWhereField, strWhereFieldVal); 
    System.out.println(" -- n--- " + n); 
    Toast.makeText(DownlaodTableActivity.this, n+" rows updated", Toast.LENGTH_SHORT).show(); 
} 

我要生成更新語句動態的方式。從這些代碼我把哪裏部分also.But我沒有產生where子句。

見:

UPDATE strTableName SET ExecutiveCode=?, FreeIssuePrefix=?, DisPaySchedulePrefix=?, NextFreeIssueNo=?, NextReturnNo=?, UploadedType=?, DisNextFOCNo=?, DisNextFreeIssueNo=? 

請幫我如何給凡化酶(下面筆者就給字符串&參數作爲字符串數組)提前

謝謝...

回答

1

嘗試這樣

dbAdapter.updateRecordsInDB(strTableName, initialValues,""+whereField+"='"+whereFieldValue+"'",null); 

如果您的whereField字段的類型是數字,那麼請勿使用'' 如果您必須與多個值進行比較,請使用

String where=""; 
for(int i=0;i<strWhereField.length();i++) 
    { 
    where=where+whereField[i]+"='"+strWhereFieldValue[i]+"'" 
    if(i<(strWhereField.length()-1)) where=where+" and" 
    } 
dbAdapter.updateRecordsInDB(strTableName, initialValues,where,null); 
+0

其中,clauses包含更多的代碼。例如'BusinessUnit =?和ExecutiveCode =? '所以參數以字符串數組形式出現 – Piraba

+0

查看更新的答案 – Rasel