Sqlite Update Query not working | Android - android

I am new to Android apps and trying to run Sqlite-query like this;
String query = "Select IsEnabled from Translation_Language ORDER BY TransLation_Language_ID";
Cursor Translations = SplashScreen.database.rawQuery(query,null);
and it is working. But i don't know how to update the same table. I have tried some methods but failed. Could anyone help me to resolve it???
Update query code
SplashScreen.database.update("Translation_Language", "IsEnabled", "TransLation_Language_ID", new String[] {"1", "8"});
I want to set IsEnabled property to 1 that have ID=8.

try this way:
ContentValues cv = new ContentValues();
cv.put("IsEnabled","1");
db.update("Translation_Language", cv, "TransLation_Language_ID=?", new String[] { "8" });

Related

How to insert records if update query failed in sqlite android

I have a case, If my update query is failed due because there is no existing data in database. Then, I want to insert new record if not available already in database. I searched everywhere, But, i did not get a solution according my source code. Find the below code and tell me the solution. Thanks advance dude.
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(PAGENUM, stepaObj.getPageno());
values.put(SCORE, stepaObj.getScore());
values.put(FIR, stepaObj.getFir());
values.put(PENALTY, stepaObj.getPenalty());
values.put(PUTTS, stepaObj.getPutts());
values.put(PLAYED, stepaObj.getPalyed());
values.put(SCORECARDID, stepaObj.getScorecardid());
values.put(ISUPDATE, stepaObj.getIsupdate());
int result = db.update(TABLE_SCORECARD_STEPA, values, SCORECARDID+" = ?",
new String[] { String.valueOf(scorecardID)});
System.out.println("updateScore by API data = "+result);
db.close();
If the update failed then the result will be 0 ( result : number of rows affected by update)
int result = db.update(TABLE_SCORECARD_STEPA, values, SCORECARDID+" = ?",
new String[] { String.valueOf(scorecardID)});
if(result <= 0){
result = db.insert(TABLE_SCORECARD_STEPA, null values);
}

Update Query not working in Android Sqlite

My Java code Update Data base Table
String qq="UPDATE ChallanItems SET Recieve ="+str+" WHERE ItemNo = "+code;
Log.d("Qry", qq);
myDbHelper.updatequery(qq);
updatequery method
public void updatequery(String qry)
{
Cursor c = myDataBase.rawQuery(qry, null);
Log.d("Up", ""+c.getCount());
}
When i updated Data base the count return 0 and table not updated
I am using this Also but not work
String qq="UPDATE ChallanItems SET Recieve ="+str+" WHERE ItemNo = "+"'"+code+"'";
Please Help Me how i can fix this problem
Thanks In Advance
Use execSQL() for such SQL and not rawQuery().
rawQuery() just compiles the SQL but does not run it. You'd need to call one of the move...() methods on the returned Cursor to execute a step of the compiled SQL program.
execSQL() both compiles and runs the SQL program.
There's also possibly a syntax problem with your literals - use parameters i.e. ? placeholders in SQL and String[] bind arguments to be safe.
To update sqlite query change
Cursor c = myDataBase.rawQuery(qry, null);
to this
myDataBase.execSQL(qry);
try to use this:
ContentValues values = new ContentValues();
values.put("Recieve", str);
db.update("ChallanItems", values2, "ItemNo = ?", new String[] { code });

Sqlite update a column based on where clause android

I am developing an android app, where I want to update a column in a row based on a where clause which comprises of two values. Below is what I have tried.
public void setlocationsetcolumn(double lats , double longs, String setvalue)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(Loc_set, setvalue);
db.update(TABLE_LATLONG, values, " WHERE "+Loc_lati +" = " + lats+" AND "+Loc_longi+" = "+longs, null);
}
I wan to update the Loc_set, based on the lats & longs values. but i am getting a force close. Am I doing something wrong here. Please help.Thanks!
below snippet will help you.
String[] args = new String[]{lats, longs};
db.update(TABLE_LATLONG, values, "Loc_lati=? AND Loc_longi=?", args);
Drop the WHERE String . Try this :
db.update(TABLE_LATLONG, values, Loc_lati +" = " + lats+" AND "+Loc_longi+" = "+longs, null);
However, I Don't know what loc_lati and loc_longi are, hopefully columns in your db.
String[] args = new String[]{lats, longs};
db.update(TABLE_LATLONG, values, "Loc_lati=? AND Loc_longi=?", args);
You can use this logic for the update of table.

Unable to delete or update a row from sqlite table for android

I would like to delete a row from sqlite table for android. I am using following code but its giving unexpected stopping your program:
SQLiteDatabase db;
db = openOrCreateDatabase("user_info.db",SQLiteDatabase.CREATE_IF_NECESSARY,null);
db.delete("checked_details", "fname = "+a, new String[]{"fname"});
my code for update is:
ContentValues values = new ContentValues();
values.put("fname", a);
values.put("checked", 0);
db.insert("checked_details", null, values);
db.update("checked_details", values, "fname = "+a, new String[]{"fname"});
I do not know where I am doing mistake...Can anyone help me to solve this problem?
U have to change the code like this
db.delete("checked_details", "fname = ' "+a+" ' ", null);

Cannot update multiple rows in Sqlite database update command in android

I am facing a problem here. I am trying to bulk update my table i.e trying to update multiple rows in the database. The update is simple. I just need to set a column value to 1 which is actually used as a flag in the app. So I want to set the value to 1 and in the where clause I give the string array of all the ids where i want it to set.
But the problem is that it gives me an "android.database.sqlite.SQLiteException: bind or column index out of range:" . However when a single value is provided either as string or an string array, the update works fine.
here is the query
db.update( tableName, cv, Z_ID + "=?", items );
where items is of type String[ ]
kindly tell me what am I missing?
regards
Fahad Ali Shaikh
That is not going to work I believe based on the one line you gave.
If we look at the method call signature :
update(String table, ContentValues values, String whereClause, String[] whereArgs)
so:
ContentValues cv=new ContentValues(); cv.put(colName, new Integer(1));
String[] items = new String []{String.valueOf(Z_ID)}
db.update( tableName, cv, Z_ID + "=?", items );
Does you code look something like this?
Your items array must match up with the where clause. A different example would look something like this:
ContentValues cv=new ContentValues(); cv.put(salesColName, new Integer(4534));
String whereClause = "band=? and album=?"; String whereArgs = new String [] { "U2", "Joshua Tree" };
db.update( tableName, cv, whereClause , whereArgs);

Categories

Resources