Android db.update where string - android

I'm using the method db.update to update the data according to the id.
now I would like at the same time change the same data that is present in another table. but the second part of the code does not work .... you have any ideas?
cv.put(CategorieTable.NOME_CATEGORIA, Ecatgoria.getText().toString());
String idc = id.getText().toString();
SQLiteDatabase db = mHelper.getWritableDatabase();
db.update(CategorieTable.TABLE_NAME, cv, idc + "=" + CategorieTable._ID, null);
//THE SECOND PART
cv.put(GiornateTable.CATEGORIA, Ecatgoria.getText().toString());
String nCategoria = Ecatgoria.getText().toString();
db.update(GiornateTable.TABLE_NAME, cv, nCategoria + "=" + GiornateTable.CATEGORIA, null);

You are using the same ContentValues instance (cv) for both operations. At second part you need to call cv.clear(), before put the new values.
cv.put(CategorieTable.NOME_CATEGORIA, Ecatgoria.getText().toString());
String idc = id.getText().toString();
SQLiteDatabase db = mHelper.getWritableDatabase();
db.update(CategorieTable.TABLE_NAME, cv, idc + "=" + CategorieTable._ID, null);
//THE SECOND PART
cv.clear(); // Clean the Content Values.
cv.put(GiornateTable.CATEGORIA, Ecatgoria.getText().toString());
String nCategoria = Ecatgoria.getText().toString();
db.update(GiornateTable.TABLE_NAME, cv, nCategoria + "=" + GiornateTable.CATEGORIA, null);

Related

Can not Update Sqlite Database with Cursor and Calling onClickListener

This is Method Cursor on DatabaseHelper;
public Cursor updateNewQuiz(int n, String s) {
return getWritableDatabase().rawQuery("UPDATE tb_characters SET LOCKED = '" + s + "' WHERE ID = '" + n + "'", null);
}
public Cursor updateThisQuiz(int n) {
return getWritableDatabase().rawQuery("UPDATE tb_characters SET ANSWERED = '1' WHERE ID = '" + n + "'", null);
}
And, I was Calling OnClickLIstener in Acticity like this
int nn = Integer.parseInt(resultes0);
int s = Integer.parseInt(resultes0);
Cursor cursor = myDb.updateNewQuiz(nn+1,"1");
Cursor cursor1 = myDb.updateThisQuiz(s);
But, my database it still Does not UPDATE...
Please Help Me ....
The documentation says:
A string constant is formed by enclosing the string in single quotes (').
So ... WHERE ID = '" + n + "' ... searches for a row that has a string as the ID. There is no such row, so nothing gets updated.
In SQL, you must write numbers without quotes:
... WHERE ID = " + n + " ...
Furthermore, you must use execSQL() to execute statements that are not SELECT queries.
It might be a better idea to use update() instead:
ContentValues cv = new ContentValues();
cv.put("LOCKED", 1);
db.update("tb_characters", cv, "ID = " + n, null);

Replace function in SQLITE

I was trying to duplicate this SQLite statement from the line of code below:
Cursor cursor = db.rawQuery("update tbl_details SET ticket = replace(ticket, " + tempID + ", " + ticket + ")", null);
to this one:
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("ticket", "replace(ticket, " + tempID + ", " + ticket + ")");
db.update("tbl_details", cv, null, null);
return true;
What I am trying to do is to get a New ID and replace all instances of the old temporary ID in the database. But the code above is changing all the records in ticket column.
Please help. Thank you!
You can use ContentValues to bind literal values only, not expressions like replace(...).
To run the raw UPDATE SQL, just use execSQL() instead of rawQuery(). rawQuery() alone won't actually run the code until the returned Cursor is moved.

getting exception in updating data base sqlite in android

to update a row in data base when i press a button
public void updateContact(long id, String x, String y,String angle)
{
ContentValues editCon = new ContentValues();
editCon.put(KEY_X, x);
editCon.put(KEY_Y, y);
editCon.put(KEY_ANGLE, angle);
ourDatabase.update(DATABASE_TABLE, editCon, KEY_ROWID + id, null);
}
and i use it entry.updateContact(1, Double.toString(db1), Double.toString(db2), Double.toString(db3));
but i get the exception: no such colum: _id1 (code1): , while compiling: UPDATE SavedTable SET position_y=?, position_x=?, position_angle=? WHERE_id1
even though i have a database with one row ( i create the entry in my oncreate method of main activity)
KEY_ROWID + id will not work as you expect! KEY_ROWID is probably a String containing _id and your id variable contains 1 which will result in _id1.
You need this:
ourDatabase.update(DATABASE_TABLE, editCon, KEY_ROWID + "=" + id, null);
or better:
ourDatabase.update(DATABASE_TABLE, editCon, KEY_ROWID + "=?", new String[]{String.valueOf(id)});
EDIT:
// pseudocode
Cursor c = ourDatabase.query(DATABASE_TABLE, null, null....);
if (c != null) {
if (c.getCount() >= 1) {
// there is an entry - just update
} else {
// no entry - create entry
}
} else {
// error? But definitively no entry so create one
}
You need to fix your update() method:
db.update(DATABASE_TABLE, editCon, KEY_ROWID + " = ?",
new String[] {String.valueOf(id)});
Problem was that you specified incorrect WHERE clause. You wrote this:
KEY_ROWID + id
this is translated on the database layer as _id1 where it was interpreted as column.
You needed _id = 1 and this is achieved by KEY_ROWID + " = ?" where placeholder will be replaced with value from string array.

how to use whereClause, whereArgs when updating a table in sqlite?

In my android application i have tried to update a table using
sqliteDatabase.updateWithOnConflict(table, values, whereClause, whereClause, conflictAlgorithm)
method but i have no clear idea about whereClause and whereClause variables.following code will not give any exception or error but the table will not be updated.
AndroidOpenDbHelper androidOpenDbHelper = new AndroidOpenDbHelper(CreateListsActivity.this);
SQLiteDatabase sqliteDatabase = androidOpenDbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(AndroidOpenDbHelper.LIST_NAME, editedKeyword);
sqliteDatabase.updateWithOnConflict(AndroidOpenDbHelper.TABLE_NAME_LISTS, values, AndroidOpenDbHelper.LIST_NAME + "=" + id,null, SQLiteDatabase.CONFLICT_IGNORE);
sqliteDatabase.updateWithOnConflict(AndroidOpenDbHelper.TABLE_NAME_KEYWORDS, values, AndroidOpenDbHelper.LIST_NAME + "=" + id, null, SQLiteDatabase.CONFLICT_IGNORE);
sqliteDatabase.updateWithOnConflict(AndroidOpenDbHelper.TABLE_NAME_TWEET, values, AndroidOpenDbHelper.LIST_NAME + "=" + id, null, SQLiteDatabase.CONFLICT_IGNORE);
sqliteDatabase.close();
any suggestion??
Your conflict algorithm is IGNORE -
"When a constraint violation occurs, the one row that contains the constraint violation is not inserted or changed"
Does AndroidOpenDbHelper.LIST_NAME have a unique constraint on it which you may be violating? Try FAIL and check the return code:
http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#CONFLICT_FAIL
Also, bind the parameters in the statement. Thats the correct way to use where clause and args -
sqliteDatabase.updateWithOnConflict(AndroidOpenDbHelper.TABLE_NAME_KEYWORDS, values, AndroidOpenDbHelper.LIST_NAME + "= ?" , new String[]{id}, SQLiteDatabase.CONFLICT_IGNORE);
you can write your own sql update statement like this:
String sql="update <tableName> set <columnName> = 'newValue' where <columnName>= 'oldValue' ";
sqliteDatabase.execSql(sql,null);

update sql database with ContentValues and the update-method

I would like to update my SQL lite database with the native update-method of the SQLiteDatabase class of android.
ContentValues dataToInsert = new ContentValues();
dataToInsert.put("name", "flo");
dataToInsert.put("location", "flotown");
String where = "id" + "=" + id;
try{
db.update(DATABASE_TABLE, dataToInsert, where, null);
}
catch (Exception e){
String error = e.getMessage().toString();
}
but I get following error:
android.database.sqlite.SQLiteException: near "15": syntax error: ,
while compiling: UPDATE mytable SET location=?, name=? WHERE
id=2010-09-21 15:05:36.995
I don´t know what should be the problem. Somehow the values do not arrive in the SQL statement. I did nearly the same with the insert method and that worked quite fine.
You're using the update function wrong. It should be like this:
String where = "id=?";
String[] whereArgs = new String[] {String.valueOf(id)};
db.update(DATABASE_TABLE, dataToInsert, where, whereArgs);
The Strings in the whereArgs array gets substituted in for each '?' in the where variable.
ie. if you had where = "name=? AND type=? then the first '?' would get replaced by whereArgs[0] and the second by whereArgs[1].
Actually, you just need to add apostrophes to your where clause. So it ought to be:
String where = "id='" + id + "'"
(note: however, this is not best practice, as it theoretically leaves open to injection attacks)
I have an other approach
public boolean updateEmployee(TalebeDataUser fav) {
SQLiteDatabase database = dbHelper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(DBHelper.COLUMN_ID, fav.getId());
contentValues.put(DBHelper.COLUM_AD, fav.getAd());
contentValues.put(DBHelper.COLUMN_NUMARA, fav.getNumara());
contentValues.put(DBHelper.COLUMN_YURD_ID, fav.getYurtID());
contentValues.put(DBHelper.COLUMN_EGITIM_ID, fav.getEgitimTur());
contentValues.put(DBHelper.COLUMN_TEL, fav.getTel());
contentValues.put(DBHelper.COLUMN_EMAIL, fav.getEmail());
contentValues.put(DBHelper.COLUMN_ADDRESS, fav.getAdres());
String whereClause = DBHelper.COLUM_AD + " = ? AND " + DBHelper.COLUMN_NUMARA + " = ? ";
final String whereArgs[] = {fav.getAd(), String.valueOf(fav.getNumara())};// old nameler taranıyor
int affectedRows = database.update(DBHelper.TABLE_NAME_OGR, contentValues, whereClause, whereArgs);
return affectedRows > 0;
}
Actually what exactly you written is correct. The syntax is correct.
But you have to check these.
String where = "id" + "=" + id;
In the above declaration "id" should be type number and id should be int.
And if id is a type of TEXT then follow #Adam javin answer.

Categories

Resources