Android更新sqlite表

时间:2011-08-03 06:08:25

标签: android sqlite

我使用dbAdapter编写了更新表。

  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();
}

我想生成更新语句动态方式。从这些代码我把Where部分也放在了。但是我没有生成where子句。

见:

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

请帮帮我如何给出Where clase(这里我给String&amp;参数作为字符串数组)

提前致谢...

1 个答案:

答案 0 :(得分: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);