Edit SQLite database record - android

How can I edit a particular record in a SQLite table given, Database Name, Table Name, int _id ,Column Name and Desired value of record?
EDIT:
My solution was database.update(DataBaseHelper.VFS_DATABASE_TABLE, values, "_id=?", new String[] {id+""});

public int deleteCpShadowEntryById (int id) {
int delRows = mDb.delete(<your_table_name>, "_id" + "=?", new String[] {String.valueOf(id)});
return delRows;
}
But I guess you do not want to delete. You want to update your value. So in this case you should at first select your row, fill the values of this row into a new ContentValues, replace you old value with new and update the row.
Edit: Maybe try this:
ContentValues args = new ContentValues();
args.put(<your_columnName>, newValue);
db.update(<your_table_name>, args, "_id" + "=?", new String[] {String.valueOf(id)});

Should be a simple bit of SQL:
DELETE FROM <table name> WHERE <column name> = <desired value>

SQLiteDatabase has a delete method :
public int delete (String table, String whereClause, String[] whereArgs)
If you have id of the particular record the code would be something like :
String whereArgs[] = new String { id };
db.delete(TABLE_NAME, "_id = ?", whereArgs);

Related

Android - SQLite Update Statement

I am playing with an example of SQLite I found on the internet. I have an update statement like this:
public int updateContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, contact.getName());
return db.update(TABLE_CONTACTS, values, KEY_ID + " = ?" +
contact.getID(), null);
}
And an update statement like this:
public int updateContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, contact.getName());
return db.update(TABLE_CONTACTS, values, KEY_ID + " = ?",
new String[]{String.valueOf(contact.getID())});
}
Can someone tell me the difference?
Here is the method signature for an update operation on SQLite database.
int android.database.sqlite.SQLiteDatabase.update(
String table, ContentValues values, String whereClause, String[] whereArgs)
From developer.android.com
You may include ?s in the where clause, which will be replaced
by the values from whereArgs. The values will be bound as Strings.
Btw your first example wouldn't work cause you have included
"KEY_ID + " = ?" + contact.getID()" in whereClause param and kept the whereArgs null. The ?s would'nt be replaced by your arg contact.getId()
Change whereClause in 1st example to this: KEY_ID + " = " + contact.getID()
On your 1st example.The code can't update anything.
The update method whereCause params will be convert to some SQL on where case ,It will replace the ? placeholder with whereArgs.
Such as:
In your 1st example.If contact.getId() return 1,The final SQL is like:
update contact set KEY_NAME = 'your contact name ' where KEY_ID = ? 1
but 2st example final SQL is like:
update contact set KEY_NAME = 'your contact name ' where KEY_ID = 1
So,the first example is not work.

Android : database not updated

I want to update a field in my database, but the changes not saved to database.
I use The "update" method in database class but not work
Please
This is my Method
public void myupdatedb(int id,String value){
ContentValues cv = new ContentValues();
cv.put("fav", "1");
mydb.update("contents", cv, "id" + "=" + id , null);
}
and in the MainActivity:
db.myupdatedb(1,"1");
From Android documentation
public int update (String table, ContentValues values, String whereClause, String[] whereArgs)
You may include ?s in the where clause, which will be replaced by the values from whereArgs. The values will be bound as Strings.
Try
mydb.update("contents", cv, "id = ?", new String[]{Integer.toString(id)});

Update row in SQlite database by row position in android

I have database which contains "date" column and "item" column.
I want that user could update specific row in the database.
I trying to do it with update method in SQLiteDatabase class.
My problem is that i dont know how to make update method find exactly the row i want.
I saw some example that use it with parameters from one word.
like this:
ourDatabase.update(tableName, cvUpdate, rowId + "=" + item , null);
My problem is that i want to update the row that have specific item and date. so the name of the item alone is not enough.
I tried this code below but its didnt work, hope youll can help me.
public void updateEntry(String item, String date) throws SQLException{
String[] columns = new String[]{myItem, myDate};
Cursor c = ourDatabase.query(tableName, columns, null, null, null, null, null);
long position;
ContentValues cvUpdate = new ContentValues();
cvUpdate.put(date, myDate);
cvUpdate.put(item, myExercise);
int itemAll = c.getColumnIndex(myItem);
int dateAll = c.getColumnIndex(myDate);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
if (c.getString(itemAll).equals(myItem) && c.getString(dateAll).equals(myDate))
{
position = c.getPosition();
break;
}
}
ourDatabase.update(tableName, cvUpdate, rowId + "=" + position , null);
}
First, the columns String[] is supposed to contain column names, such as "_ID", or whatever are the column names you have used. Given that you compare the content of the column myItem with the object myItem, I assume there is a confusion somewhere here.
Secondly, rowId and position are different things in SQL, especially if you delete rows, as the row id usually is autoincrement, and especially since your query is not explicitely sorted. Replacing c.getPosition() by c.getLong(c.getColumnIndex(ID_COLUMN)) would make more sense.
Thirdly, sql is nice because you can query it. For example, rather than get all items and loop to find the matching date and item, you can :
String whereClause = ITEM_COLUMN + " = ? and " + DATE_COLUMN + " = ?";
String[] whereArgs = new String[] { item, date };
Cursor c = ourDatabase.query(tableName, columns, whereClause, whereArgs, null, null, null);
instead of your for loop.
Forthly, you can even make the query in the update :
String whereClause = ITEM_COLUMN + " = ? and " + DATE_COLUMN + " = ?";
String[] whereArgs = new String[] { item, date };
ourDatabase.update(tableName, cvUpdate, whereClause, whereArgs);
Extra tip: use full caps variable names for contants such as column names, it help with readability.

How to use SQLite's "whereClause" inside update method

I'm dealing with an sqlite db in android application.
To update a row in the db I use method of SQLiteDatabase object "update"
SQLiteDatabase.update(String table, ContentValues values, String whereClause, String[] whereArgs)
But here a confusion, how "whereClause" should look like?
Assume I have these values for update.
values.put("name", appInfo.getName());
values.put("package_name", appInfo.getPackageName());
values.put("version_name", appInfo.getVersionName());
I want to update a row where package_mane column equal to "com.mynamespace.db". How "whereClause" should be written here? Thanks.
db.update("table", values, "package_name = ?", new String[]{"com.mynamespase.db"});
Another example:
db.update("table", values, "package_name = ? and other_field = ?", new String[]{"com.mynamespase.db", "test"});
You whereClause should look like this:
String whereClause = "package_mane = ?"; // package_name perhaps?
String[] whereArgs = new String[]{"com.mynamespase.db"};
String from whereArgs are used to substitute ? in whereClause.
You should use it like this:
SQLiteDatabase.update(table, "package_name=?", new String[] { "com.mynamespace.db" });
and prevent other problems and cache inefficiencies if you concat the string.
db.update(TABLE_NAME, values, "package_name=com.mynamespase.db", null);
// or
String[] args = { "com.mynamespase.db" };
db.update(TABLE_NAME, values, "package_name=?", args);

No result on sqlite query

Hi all
I create a table like that
private static final String CREATE_TABLE_PRODUCT =
"create table prod (id integer primary key,titre text not null, desc text, is_free integer);";
i update it this way
SQLiteDatabase db = getConnection();
ContentValues updateEvent = new ContentValues();
updateEvent.put("is_free", 1);
int ok = db.update(prod, updateEvent, "id=?",
new String[] { Long.toString(evenement.getId()) });
db.close();
I do see the change in eclipse with DDMS questoid
but when i try to retrieve the value I get nothing ....
Cursor c = db.query(prod, new String[] { "id",
"titre", "desc", "is_free", },
"is_free=?", new String[] {"1"}, null, null,
null);
I've tried some variant like
String query = "SELECT * FROM "+EVENEMENT_TABLE+" WHERE is_free=1;";
Cursor c = sdb.rawQuery(query, null);
with no success
is this problem come from the type (integer) of my column ?
Any help
Do not use reserved keywords like desc in your queries. This probably causes query to fail.
In case you don't know update will not add values to the table. Use insert instead.

Categories

Resources