开发者

Android create Insert statement at runtime

开发者 https://www.devze.com 2023-03-25 23:35 出处:网络
I have fileds list & values list.I want to insert records using DBAdapter.I tried from this link http://mfarhan133.wordpress.com/2010/10/24/database-crud-tutorial-for-android/

I have fileds list & values list.I want to insert records using DBAdapter.I tried from this link http://mfarhan133.wordpress.com/2010/10/24/database-crud-tutorial-for-android/

  DBAdapter dbAdapter = DBAd开发者_StackOverflowapter.getDBAdapterInstance(Insert.this);
  dbAdapter.openDataBase();


ContentValues initialValues = new ContentValues();
initialValues.put("name", etName.getText().toString());
initialValues.put("age", etAge.getText().toString());
long n = dbAdapter.insertRecordsInDB("user", null, initialValues);
Toast.makeText(Insert.this, "new row inserted with id = " + n, Toast.LENGTH_SHORT).show();

Please anybody help me how to send the fields & their values at runtime.Please help me as soon as possible


You can make fields as String array & their value also make array.For example:

 String[] strToFields  = new String[names.length()];
 String[] strToFieldsVal  = new String[names.length()];

    for (int k = 0; k < names.length(); k++) {
        strToFields[k] = names.getString(k);
            strToFieldsVal[k]=strVal;
    }

calling method like insertTableRecords(actualtable, strToFields, strToFieldsVal);

method should be:

  public void insertTableRecords(String strTableName, String[] strToFields, String[] strValues){
    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.insertRecordsInDB(strTableName, null, initialValues);
    System.out.println( " -- inserted status : --- " + n);
}

If you need more help see this Android sqlite dynamic insert query

0

精彩评论

暂无评论...
验证码 换一张
取 消