Sqlite in Android How to Update a Specific Row

SQLite in Android How to update a specific row

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

Updating a specific row value in sqlite database

Execute this query (it changes the value of a with z):

UPDATE myTable SET [NAME] = 'z' WHERE [NAME] = 'a'

I guess that NAME is a reserved keyword, so, I enclosed it in []

Update values of a specific column of a specific row. Android SQLite

The easiest way for you to achieve this would be to use a SQL update query as follows:

From the SQLite Web Site:

The SQLite UPDATE Query is used to modify the existing records in a table. You can use a WHERE clause with UPDATE query to update selected rows, otherwise all the rows would be updated.

The syntax for the update query is as follows:

 UPDATE table_name
SET column1 = value1, column2 = value2...., columnN = valueN
WHERE [condition];

So in your case your sql update query would look some thing like this:

UPDATE ORDERTABLE SET QUANTITY = (INSERT VALUE OF YOUR EDIT TEXT) WHERE NAME =   'Order2'

You can then execute your query by using the execSQL() method of your SQLiteDatabase object that you have and passing in the sql query above as the string parameter.

Android Sqlite update first row only

try this:

String strSQL = "UPDATE table SET column = something WHERE id IN (SELECT id FROM table WHERE name = john AND age = 10 LIMIT 1)";

Android SQLite, update specific field from specific row

Looks like it's complaining about the email address, perhaps the @.

try

int i = db.update(TABLE_FAVORITES, args, EMAIL + "= ' " + email + "'", null);

ie. single quotes around the email address.

Updating single row in SQLite Android Studio

There is nothing intrinsically wrong with the code that you have shown. The example below, which utilises your exact code that you have shown, works.

As such your issue is either with other unprovided code or with the method you are using to determine that no data has been updated.

For this test the following was the code for the class that is subclass of SQLiteOpenHelper, in this case ServiceDBHelper.java :-

public class ServiceDBHelper extends SQLiteOpenHelper {

public static final String DBNAME = "service.db";
public static final int DBVERSION = 1;
public static final String TABLE_SERVICE = "service";
public static final String COLUMN_NAME = "name";
public static final String COLUMN_RATE = "rate";

public ServiceDBHelper(Context context) {
super(context, DBNAME, null, DBVERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {

String crt_sql = "CREATE TABLE IF NOT EXISTS " + TABLE_SERVICE + "(" +
COLUMN_NAME + " TEXT PRIMARY KEY, " +
COLUMN_RATE + " TEXT" +
")";
db.execSQL(crt_sql);
}

@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {

}

public long insertService(String name, String price) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(COLUMN_NAME,name);
cv.put(COLUMN_RATE,price);
return db.insert(TABLE_SERVICE,null,cv);
}

public boolean modiService(String name, String price, String updatename, String updateprice ){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(COLUMN_NAME, updatename);
cv.put(COLUMN_RATE, updateprice);
db.update(TABLE_SERVICE, cv, COLUMN_NAME + " = ?" , new String[] {name});
return true;
}

public void logService(String description) {
String TAG = "LOGSERVICE";
Log.d(TAG,"Logging information for the Service Table for " + description);
SQLiteDatabase db = this.getWritableDatabase();
Cursor csr = db.query(TABLE_SERVICE,null,null,null,null,null,null);
Log.d(TAG,"Number of rows in the " + TABLE_SERVICE + " table is " + String.valueOf(csr.getCount()));
while (csr.moveToNext()) {
Log.d(TAG,
"Column " + COLUMN_NAME + " has a value of " + csr.getString(csr.getColumnIndex(COLUMN_NAME)) +
". Column " + COLUMN_RATE + " has a value of " + csr.getString(csr.getColumnIndex(COLUMN_RATE))
);
}
csr.close();
}
}
  • As you can see the modiService method is as per you code.
  • Other code has been added to :-

    • Create the table (named service) when the database is created.
    • Insert rows into the table to add some test data.
    • Write to data from the table to the log.

The following is the code used in an Activity to
- insert some rows,
- display the rows
- update (modify a row)
- display the rows

The code used in MainActivity.java was :-

public class MainActivity extends AppCompatActivity {

ServiceDBHelper mDBHlpr;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDBHlpr = new ServiceDBHelper(this);
mDBHlpr.insertService("Fred","125.45");
mDBHlpr.insertService("Mary","99.75");
mDBHlpr.insertService("Harry","245.34");
mDBHlpr.logService("After initial inserts");
mDBHlpr.modiService("Mary","99.75","Susan","333.33");
mDBHlpr.logService("After Updating Mary to Susan");
}
}

The relevant output in the Log was :-

11-18 06:51:02.303 1212-1212/so53291104.so53291104 D/LOGSERVICE: Logging information for the Service Table for After initial inserts
11-18 06:51:02.303 1212-1212/so53291104.so53291104 D/LOGSERVICE: Number of rows in the service table is 3
11-18 06:51:02.303 1212-1212/so53291104.so53291104 D/LOGSERVICE: Column name has a value of Fred. Column rate has a value of 125.45
11-18 06:51:02.303 1212-1212/so53291104.so53291104 D/LOGSERVICE: Column name has a value of Mary. Column rate has a value of 99.75
11-18 06:51:02.303 1212-1212/so53291104.so53291104 D/LOGSERVICE: Column name has a value of Harry. Column rate has a value of 245.34
11-18 06:51:02.307 1212-1212/so53291104.so53291104 D/LOGSERVICE: Logging information for the Service Table for After Updating Mary to Susan
11-18 06:51:02.307 1212-1212/so53291104.so53291104 D/LOGSERVICE: Number of rows in the service table is 3
11-18 06:51:02.307 1212-1212/so53291104.so53291104 D/LOGSERVICE: Column name has a value of Fred. Column rate has a value of 125.45
11-18 06:51:02.307 1212-1212/so53291104.so53291104 D/LOGSERVICE: Column name has a value of Susan. Column rate has a value of 333.33
11-18 06:51:02.307 1212-1212/so53291104.so53291104 D/LOGSERVICE: Column name has a value of Harry. Column rate has a value of 245.34

As can be seen the row Mary 99.75 has been changed using the modiService method to Susan 333.33.

How do I update a specific row in a specific column in SQlite in Java?

Your condition:

"Username="+"Username"

is equivalent to:

"Username=Username"

which is always TRUE and this is why you update all the rows of the table.

Your method's updateCompletedSessions() 2nd argument is u which is an object of type User and I assume there is a method defined for the class User like getName() or something similar.

If this is the case then use it like this:

database.update(TABLE_NAME, cv, "Username=?", new String[] {u.getName()});

Always use placeholders ? and pass their values in the 4th argumement of update() instead of concatenating the arguments.

How to update data in a SQLite database with an AND condition?

Your can write in two ways :

long id = db.update(tableName, contentValues, "hhid=" + " = ? AND " + "respid=" + " = ?",new String[]{hhid_id, resp_id});

OR

db.update(SSFormat_One, contentValues, "hhid=" + hhid_id + " and respid=" + resp_id , null);


Related Topics



Leave a reply



Submit