score:317

Accepted 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 = ?", new string[]{id});

score:0

public long filldatatempo(string table){
    string[] table = new string[1];
    tabela[0] = table; 
    contentvalues args = new contentvalues();
    args.put(dbopenhelper.data_hora, new date().tostring());
    args.put(dbopenhelper.nome_tabela, nome_tabela);
    return db.update(database_table, args, string + " like ?" ,tabela);
}

score:0

just give rowid and type of data that is going to be update in contentvalues.

public void updatestatus(string id , int status){

sqlitedatabase db = this.getwritabledatabase();

contentvalues data = new contentvalues();

data.put("status", status);

db.update(tablename, data, "columnname" + " = "+id , null);

}

score:1

just try this way

  string strfilter = "_id=" + id;
  contentvalues args = new contentvalues();
  args.put(key_title, title);
  mydb.update("titles", args, strfilter, null);**

score:1

method for updation in sqlite:

public void updatemethod(string name, string updatename){
    string query="update students set email = ? where name = ?";
    string[] selections={updatename, name};
    cursor cursor=db.rawquery(query, selections);
}

score:1

i will demonstrate with a complete example

create your database this way

    import android.content.context
    import android.database.sqlite.sqlitedatabase
    import android.database.sqlite.sqliteopenhelper

    class dbhelper(context: context) : sqliteopenhelper(context, database_name, null, database_version) {
        override fun oncreate(db: sqlitedatabase) {
            val createproductstable = ("create table " + business.table + "("
                    + business.idkey + " integer primary key autoincrement ,"
                    + business.key_a + " text, "
                    + business.key_b + " text, "
                    + business.key_c + " text, "
                    + business.key_d + " text, "
                    + business.key_e + " text )")
            db.execsql(createproductstable)
        }
        override fun onupgrade(db: sqlitedatabase, oldversion: int, newversion: int) {
            // drop older table if existed, all data will be gone!!!
            db.execsql("drop table if exists " + business.table)
            // create tables again
            oncreate(db)

        }
        companion object {
            //version number to upgrade database version
            //each time if you add, edit table, you need to change the
            //version number.
            private val database_version = 1

            // database name
            private val database_name = "business.db"
        }
    }

then create a class to facilitate crud -> create|read|update|delete

class business {
    var a: string? = null
    var b: string? = null
    var c: string? = null
    var d: string? = null
    var e: string? = null

    companion object {
        // labels table name
        const val table = "business"
        // labels table columns names
        const val rowidkey = "_id"
        const val idkey = "id"
        const val key_a = "a"
        const val key_b = "b"
        const val key_c = "c"
        const val key_d = "d"
        const val key_e = "e"
    }
}

now comes the magic

import android.content.contentvalues
import android.content.context

    class sqlitedatabasecrud(context: context) {
        private val dbhelper: dbhelper = dbhelper(context)

        fun updatecart(id: int, mbusiness: business) {
            val db = dbhelper.writabledatabase
            val valuetochange = mbusiness.e
            val values = contentvalues().apply {
                put(business.key_e, valuetochange)
            }
            db.update(business.table, values, "id=$id", null)
            db.close() // closing database connection
        }
    }

you must create your productsadapter which must return a cursoradapter

so in an activity just call the function like this

internal var cursor: cursor? = null
internal lateinit var mproductsadapter: productsadapter

 msqlitedatabasecrud = sqlitedatabasecrud(this)
    try {
        val mbusiness = business()
        mproductsadapter = productsadapter(this, c = todocursor, flags = 0)
        lstproducts.adapter = mproductsadapter


        lstproducts.onitemclicklistener = onitemclicklistener { parent, view, position, arg3 ->
                val cur = mproductsadapter.getitem(position) as cursor
                cur.movetoposition(position)
                val id = cur.getint(cur.getcolumnindexorthrow(business.idkey))

                mbusiness.e = "this will replace the 0 in a specific position"
                msqlitedatabasecrud?.updatecart(id ,mbusiness)

            }

        cursor = databasemcrud!!.productslist
        mproductsadapter.swapcursor(cursor)
    } catch (e: exception) {
        log.d("exceptionadapter :",""+e)
    }

enter image description here

score:1

sqlitedatabase mydb = this.getwritabledatabase();

contentvalues cv = new contentvalues();
cv.put(key1,value1);    
cv.put(key2,value2); /*all values are your updated values, here you are 
                       putting these values in a contentvalues object */
..................
..................

int val=mydb.update(tablename, cv, key_name +"=?", new string[]{value});

if(val>0)
 //successfully updated
else
 //updation failed

score:1

here i have completed this kind of code for update the row of a database, this is the code of database handler class

public boolean updatedata(string id,string name,string age,string gender){
    sqlitedatabase sqlitedatabase = this.getwritabledatabase();
    contentvalues contentvalues = new contentvalues();
    contentvalues.put(id,id);
    contentvalues.put(name,name);
    contentvalues.put(age,age);
    contentvalues.put(gender,gender);

    sqlitedatabase.update(table_name,contentvalues,id+"= ?",new string[]{id});
    return true;           //boolean value return korbe
}

score:2

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}

score:2

for updates, need to call settransactionsuccessfull for changes to get committed like so:

db.begintransaction();
try {
    db.update(...) 
    db.settransactionsuccessfull(); // changes get rolled back if this not called
} finally {
   db.endtransaction(); // commit or rollback
}

score:2

//here is some simple sample code for update

//first declare this

private databaseapphelper dbhelper;
private sqlitedatabase db;

//initialize the following

dbhelper=new databaseapphelper(this);
        db=dbhelper.getwritabledatabase();

//updation code

 contentvalues values= new contentvalues();
                values.put(databaseapphelper.key_pedname, ped_name);
                values.put(databaseapphelper.key_pedphone, ped_phone);
                values.put(databaseapphelper.key_pedlocation, ped_location);
                values.put(databaseapphelper.key_pedemail, ped_emailid);
                db.update(databaseapphelper.table_name, values,  databaseapphelper.key_id + "=" + ?, null);

//put ur id instead of the 'question mark' is a function in my shared preference.

score:2

 public void updaterecord(contactmodel contact) {
    database = this.getreadabledatabase();
    contentvalues contentvalues = new contentvalues();
    contentvalues.put(column_first_name, contact.getfirstname());
    contentvalues.put(column_last_name, contact.getlastname());
    contentvalues.put(column_number,contact.getnumber());
    contentvalues.put(column_balance,contact.getbalance());
    database.update(table_name, contentvalues, column_id + " = ?", new string[]{contact.getid()});
    database.close();
}

score:3

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

score:5

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

score:6

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

score:7

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

score:14

you can try this...

db.execsql("update db_table set your_column='newvalue' where id=6 ");

score:26

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

score:44

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.

score:54

simple way:

string strsql = "update mytable set column1 = somevalue where columnid = "+ somevalue;

mydatabase.execsql(strsql);

Related Query

More Query from same tag