Responsive Ads Here

ads

2019년 8월 2일 금요일

SQLite in Android How to update a specific row?

Question

I've been trying to update a specific row for a while now, and it seems that there are two ways to do this. From what I've read and tried, you can just use the:
execSQL(String sql) method
or the:
update(String table, ContentValues values, String whereClause, String[] whereArgs) method.
(Let me know if this is incorrect as I am new to android and very new to SQL.)
So let me get to my actual code.
myDB.update(TableName, "(Field1, Field2, Field3)" + " VALUES ('Bob', 19, 'Male')", "where _id = 1", null);
I am trying to accomplish this:
Update Field1, Field2, and Field3 where the primary key (_id) is equal to 1.
Eclipse gives me a red line right underneath the word "update" and gives me this explanation:
The method update(String, ContentValues, String, String[]) in the type SQLiteDatabase is not applicable for the arguments (String, String, String, null)
I'm guessing I'm not assigning the ContentValues correctly. Can anyone point me in the right direction?


answer
First make a ContentValues object :
ContentValues cv = new ContentValues();
cv.put("Field1","Bob"); //These Fields should be your String values of actual column names
cv.put("Field2","19");
cv.put("Field2","Male");
Then use the update method, it should work now:
myDB.update(TableName, cv, "_id="+id, null);

answer
Simple way:
String strSQL = "UPDATE myTable SET Column1 = someValue WHERE columnId = "+ someValue;

myDataBase.execSQL(strSQL);

answer
At first create a ContentValues object :
ContentValues cv = new ContentValues();
cv.put("Field1","Bob");
cv.put("Field2","19");
Then use the update method. Note, the third argument is the where clause. The "?" is a placeholder. It will be replaced with the fourth argument (id)
myDB.update(MY_TABLE_NAME, cv, "_id = ?", new String[]{id});
This is the cleanest solution to update a specific row.

answer
  1. I personally prefere .update for its convenience. But execsql will work same.
  2. You are right with your guess that the problem is your content values. You should create a ContentValue Object and put the values for your database row there.
This code should fix your example:
 ContentValues data=new ContentValues();
 data.put("Field1","bob");
 data.put("Field2",19);
 data.put("Field3","male");
 DB.update(Tablename, data, "_id=" + id, null);

answer
you can try this...
db.execSQL("UPDATE DB_TABLE SET YOUR_COLUMN='newValue' WHERE id=6 ");

answer
use this code in your DB `
public boolean updatedetails(long rowId,String name, String address)
      {
       ContentValues args = new ContentValues();
       args.put(KEY_ROWID, rowId);          
       args.put(KEY_NAME, name);
       args.put(KEY_ADDRESS, address);
       int i =  mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null);
    return i > 0;
     }
for updating in your sample.java use this code
  //DB.open();

        try{
              //capture the data from UI
              String name = ((EditText)findViewById(R.id.name)).getText().toString().trim();
              String address =(EditText)findViewById(R.id.address)).getText().toString().trim();

              //open Db
              pdb.open();

              //Save into DBS
              pdb.updatedetails(RowId, name, address);
              Toast.makeText(this, "Modified Successfully", Toast.LENGTH_SHORT).show();
              pdb.close();
              startActivity(new Intent(this, sample.class));
              finish();
        }catch (Exception e) {
            Log.e(TAG_AVV, "errorrrrr !!");
            e.printStackTrace();
        }
    pdb.close();

answer
hope this'll help you:
public boolean updatedetails(long rowId, String address)
  {
     SQLiteDatabase mDb= this.getWritableDatabase();
   ContentValues args = new ContentValues();
   args.put(KEY_ROWID, rowId);          
   args.put(KEY_ADDRESS, address);
  return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null)>0;   
 }

answer
You try this one update method in SQLite
int id;
ContentValues con = new ContentValues();
con.put(TITLE, title);
con.put(AREA, area);
con.put(DESCR, desc);
con.put(TAG, tag);
myDataBase.update(TABLE, con, KEY_ID + "=" + id,null);

answer
Can try like this:
ContentValues values=new ContentValues();
values.put("name","aaa");
values.put("publisher","ppp");
values.put("price","111");

int id=sqdb.update("table_name",values,"bookid='5' and booktype='comic'",null);

answer
if your sqlite row has a unique id or other equivatent, you can use where clause, like this
update .... where id = {here is your unique row id}












댓글 없음:

댓글 쓰기

Does Python have a string 'contains' substring method?

Quastion I'm looking for a  string.contains  or  string.indexof  method in Python. I want to do: if not somestring . contain...

AD