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});
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)";
SQLite Update Statement Is Not Working
Use execSQL
instead of rawQuery
:
execSQL("UPDATE " + table_name_ + " SET " + current_stock + " = " + current_stock - sub_stock + " where item_name = '" + item_name + "'");
Difference between execSql
and rawQuery
:
execSql
Execute a single SQL statement that is NOT a SELECT or any other SQL statement that returns data.
It has no means to return any data (such as the number of affected
rows). Instead, you're encouraged to use insert(String, String,
ContentValues), update(String, ContentValues, String, String[]), et
al, when possible.
rawQuery
Runs the provided SQL and returns a Cursor over the result set.
using update statement in sqlite in android
You can update it using this code:
DatabaseCreator.class:
public class DatabaseCreator extends SQLiteOpenHelper{
private static final String DB_NAME="database_name";
private static final int DB_VER=1;
private static final String TABLE_NAME="<table creation query>";
public DatabaseCreator(Context context) {
super(context,DB_NAME, null, DB_VER);
}
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(TABLE_NAME);
}
@Override
public void onUpgrade(SQLiteDatabase database, int arg1, int arg2) {
database.execSQL("DROP TABLE IF EXISTS table_name");
onCreate(database);
}
}
Now use below code where you need to update the row:
DatabaseCreator dbcreator=new DatabaseCreator(context);
SQLiteDatabase sqdb=dbcreator.getWritableDatabase();
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);
SQLite (Android) : UPDATE query with ORDER BY
As pointed out by many, this is definitely not the correct way to go.
But I found workaround in case someone else is looking for a similar implementation.
Here it is :
UPDATE myTable SET id = - (id + 1) WHERE id > 1;
UPDATE myTable SET id = - id WHERE id < 0;
This is a hack which I found here.
Again, this is not the correct way to go. But just posting a working solution I found.
Android sqlite database no such column when update query
During update your column
name is not match with table actual column
name. Try below:
String sql = "UPDATE PRINCIPAL SET selectname=?, eventname=?, eventorg=?, eventdays=?, eventstart=?, image=? WHERE id=? ";
There are two mismatch in column between create
and update
sql
name
should beselectname
eventorganization
should beeventorg
Also your table name PRINCIPALS
not match with PRINCIPAL
.
Related Topics
How to Find the Logs on Android Studio
Check Whether Activity Is Active
Does the Web View on Android Support Ssl
Run Code Only Once After an Application Is Installed on Android Device
How to Get Retrofit Success Response Status Codes
How to Fix: "You Need to Use a Theme.Appcompat Theme (Or Descendant) with This Activity"
Listview in Arrayadapter Order Get's Mixed Up When Scrolling
Android Keyboard Hides Edittext
Disabling Android O Auto-Fill Service for an Application
Get Listview Item Position on Button Click
Work Around Canvas.Clippath() That Is Not Supported in Android Any More
How to Store SQLite Database Directly on Sdcard
Android Exception: Did You Forget to Call 'Public Void Setup (Localactivitymanager Activitygroup)'
Pass Arraylist<? Implements Parcelable> to Activity
Query If Android Database Exists!