How to update data in Sqlite? - android

In my android application I am using sqlite database. I use the following code to update data but the data is not updated.
public int setCurrentLevel(int level)
{
//update table level set currentlevel = level;
int slevel = level;
Log.d("QUIZ APP", "inside on setcreatelevel is "+slevel);
ContentValues args = new ContentValues();
args.put("currentlevel", slevel);
return db.update("level", args, "currentlevel" + ">=" + slevel, null);
}

Here you go
db.update("level",args,"currentlevel>= ?", new String[] { String.valueOf(level) });

Use this kind of code inside your function
public boolean updateTitle(long rowId, String isbn,
String title, String publisher)
{
ContentValues args = new ContentValues();
args.put(KEY_ISBN, isbn);
args.put(KEY_TITLE, title);
args.put(KEY_PUBLISHER, publisher);
return db.update(DATABASE_TABLE, args,
KEY_ROWID + "=" + rowId, null) > 0;
}

Try This
public int setCurrentLevel(int level)
{
//update table level set currentlevel = level;
int slevel = level;
Log.d("QUIZ APP", "inside on setcreatelevel is "+slevel);
ContentValues args = new ContentValues();
args.put("currentlevel", slevel);
return db.update("level", args,
"currentlevel" + ">=" + slevel, null)>0;
}

use this query......... also mention some condition on which you want to fire this update
db.execSQL("UPDATE "+tableName+" SET "+columnNameValue+" WHERE "+condition+"");
your query will look something like:
db.execSQL("UPDATE level SET currentlevel="+slevel +" WHERE currentlevel \\>=" + slevel+"");

Try this,
ContentValues cv = new ContentValues();
cv.put("Field", value);
db.update("Table", cv, "Field='value'", null);

Related

What is wrong with my SQLite update query

My SQLite update query doesn't work, but returns no error...
public void updateData(String stickerPackTitle, String installed) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(COL_INSTALLED, installed);
db.update(TABLE_NAME, cv, "' " + COL_PACK_NAME + " = " + stickerPackTitle + " '", null);
}
Replace update line with this:
db.update(TABLE_NAME, cv, COL_PACK_NAME + " = '" + stickerPackTitle + "'", null);
Below is the right method to use update query in SQLite. You should keep where clause and your where arguments in different parameters. And yes, don't forgot to close the database connection.
db.update(TABLE_NAME, cv, COL_PACK_NAME + " =?", new String[] stickerPackTitle});
db.close();
Try this type of code :
public void update_PrimaryOffer(PrimaryOffer p)
{
// TODO Auto-generated method stub
ContentValues contentValues = new ContentValues();
contentValues.put("businessId", p.getBusinessId());
contentValues.put("primaryOfferDiscount", p.getPrimaryOfferDiscount());
contentValues.put("offerImage", p.getOfferName());
contentValues.put("businessType", p.getBusinessType());
contentValues.put("businessName", p.getBusinessName());
contentValues.put("businessInformation", p.getBusinessInformation());
contentValues.put("businessImage", p.getBusinessImage());
contentValues.put("offerName", p.getOfferName());
contentValues.put("offerAddress", p.getOfferAddress());
contentValues.put("phoneNumber", p.getPhoneNumber());
contentValues.put("originalPrice", p.getOriginalPrice());
contentValues.put("expiryDate", p.getExpiryDate());
contentValues.put("latitude", p.getLatitude());
contentValues.put("longitude", p.getLongitude());
contentValues.put("likeStatus", p.getLikeStatus());
contentValues.put("favoriteStatus", p.getFavoriteStatus());
contentValues.put("likeCount", p.getLikeCount());
// contentValues.put("DateTime", p.getDateTime());
Cursor cursor = sqLiteDatabase.rawQuery("select * from "+TABLE_RECENT_PRIMARY_OFFER+";", null);
if (cursor.moveToFirst()) {
do {
int ii = sqLiteDatabase.update(TABLE_RECENT_PRIMARY_OFFER, contentValues, "businessId="+ p.getBusinessId(),null);
System.out.println("SQLITE UPDATE SCHEDULER---->" + ii);
}while(cursor.moveToNext());
}
cursor.close();
}

Android sqlite update row by calling method

I'm trying to update a row in my sqlite database but i cant find out what is wrong... I'm using this method id my DbAdapter :
public boolean updateRow(long rowId, String TimeDone, String ActDone) {
String where = KEY_ROWID + "=" + rowId;
ContentValues newValues = new ContentValues();
newValues.put(KEY_TIME, TimeDone);
newValues.put(KEY_ACT, ActDone);
// Insert it into the database.
return db.update(DATABASE_TABLE, newValues, where, null) != 0;
}
And in my Activity i try to update a row by doing this :
String a = "20:34:23";
String item2 = spnr.getSelectedItem().toString();
long rowId = spnr.getSelectedItemId();
ContentValues newValues = new ContentValues();
newValues.put("Time", a);
newValues.put("Activity",item2);
myDb.updateRow(rowId, a, item2);
Am i forgetting something , or i'm doing something wrong ?
Thanks in advance!
EDIT:
I have tried this way;
public boolean updateRow(long rowId, String TimeDone, String ActDone) {
String where = KEY_ROWID + "= ?" + rowId;
ContentValues newValues = new ContentValues();
newValues.put(KEY_TIME, TimeDone);
newValues.put(KEY_ACT, ActDone);
// Insert it into the database.
return db.update(DATABASE_TABLE, newValues, where, null) != 0;
}
But i get the following error :
12-20 17:45:12.521: E/AndroidRuntime(1979): android.database.sqlite.SQLiteException: variable number must be between ?1 and ?999 (code 1): , while compiling: UPDATE MainTable SET Activity=?,Time=? WHERE _id= ?0
And if i do this way :
public boolean updateRow(long rowId, String TimeDone, String ActDone) {
ContentValues newValues = new ContentValues();
newValues.put(KEY_TIME, TimeDone);
newValues.put(KEY_ACT, ActDone);
return db.update(DATABASE_TABLE,newValues, KEY_ROWID + " = ?",
new String[] { String.valueOf(rowId) })!= 0;
}
I get no errors but the row dosent update .
This line is not giving you the correct ID:
long rowId = spnr.getSelectedItemId();
Instead, call getSelectedItem() and get your ID from the object.
Did you forget to put ? in String where = KEY_ROWID + "=" + rowId;
Then change it to String where = KEY_ROWID + "= ?" + rowId;
or try something like this:
public int updateRow(long rowId, String TimeDone, String ActDone) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues newValues = new ContentValues();
newValues.put(KEY_TIME, TimeDone);
newValues.put(KEY_ACT, ActDone);
return db.update(DATABASE_TABLE,newValues, KEY_ROWID + " = ?",
new String[] { String.valueOf(rowID) });
}
Hope it may help.

Android db.update SQLite

I have a small problem with the method db.update. I need to change a string that corresponds to that received by the query. For example from the query I get the string "hello", if the change in "hello1" must change all the strings "hello".
In my Cursor I have name_s = c.getString(3);
And this is my update:
cv.put(Table1.ABC, Ecia.getText().toString());
db.update(Table1.TABLE_NAME, cv, Table1.ABC+ " = ?", new String[] { name_s});
try this :
String newval=Ecia.getText().toString();
String name_s = c.getString(3);
setMyField(name_s , newval);
public int setMyField(String currvalue , String newvalue) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(Table1.ABC, newvalue);
// updating row
return db.update(Table1.TABLE_NAME, values, Table1.ABC + " = ?",
new String[] { currvalue });
}

Change a value in a column in sqlite

I need to update a value in a column from a certain table. I tried this :
public void updateOneColumn(String TABLE_NAME, String Column, String rowId, String ColumnName, String newValue){
String sql = "UPDATE "+TABLE_NAME +" SET " + ColumnName+ " = "+newValue+" WHERE "+Column+ " = "+rowId;
db.beginTransaction();
SQLiteStatement stmt = db.compileStatement(sql);
try{
stmt.execute();
db.setTransactionSuccessful();
}finally{
db.endTransaction();
}
}
and I call this method like this :
db.updateOneColumn("roadmap", "id_roadmap",id,"sys_roadmap_status_mobile_id", "1");
which means that I want to set the value 1 in the column sys_roadmap_status_mobile_id when id_roadmap = id.
The problem is that nothing happens. Where is my mistake?
Easy solution:
String sql = "UPDATE "+TABLE_NAME +" SET " + ColumnName+ " = '"+newValue+"' WHERE "+Column+ " = "+rowId;
Better solution:
ContentValues cv = new ContentValues();
cv.put(ColumnName, newValue);
db.update(TABLE_NAME, cv, Column + "= ?", new String[] {rowId});
The below solution works for me for updating single row values:
public long fileHasBeenDownloaded(String fileName)
{
SQLiteDatabase db = this.getWritableDatabase();
long id = 0;
try {
ContentValues cv = new ContentValues();
cv.put(IFD_ISDOWNLOADED, 1);
// The columns for the WHERE clause
String selection = (IFD_FILENAME + " = ?");
// The values for the WHERE clause
String[] selectionArgs = {String.valueOf(InhalerFileDownload.fileName)};
id = db.update(TABLE_INHALER_FILE_DOWNLOAD, cv, selection, selectionArgs);
}
catch (Exception e) {
e.printStackTrace();
}
return id;
}

Update Function in android SQLite is not working

In my application I need to add and edit data to the SQLite database. When I do update data function app do not give any errors but my database wont update. Here is my Update function. I search in the web for two days but couldn't make this. Please help me.
public long updateEvent(String id, String title, String description, String reminder, String alarm, Date date) {
try {
int rowid = Integer.parseInt(id);
Log.i("com.eventmanager", "insert Event");
formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String datestring = formatter.format(date);
ContentValues cv = new ContentValues();
cv.put(KEY_TITLE, title);
cv.put(KEY_DESCRIPTION, description);
cv.put(KEY_REMINDER, reminder);
cv.put(KEY_ALARM, alarm);
cv.put(KEY_DATE, datestring);
myDB.beginTransaction();
Log.i("com.eventmanager","SQL UPDATE ");
myDB.update(DATABASE_TABLE, cv, KEY_ROWID + "=" + rowid, null);
myDB.setTransactionSuccessful();
myDB.endTransaction();
} catch (Exception e) {
e.printStackTrace();
}
return 1;
}
Thanks in advance !!
There may be a problem in the update statement, Try:
long i = myDB.update(DATABASE_TABLE, cv, KEY_ROWID + "=?", new String[]{rowid});
if(i>0)
return 1; // 1 for successful
else
return 0; // 0 for unsuccessful
And yes go through the public int update (String table, ContentValues values, String whereClause, String[] whereArgs) function.
Your function returns 1; that's not ok... it should return ContentValues cv
return db.update(DATABASE_TABLE, cv, KEY_ROWID+"="+rowId, null)

Categories

Resources