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)
Related
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.
I want to update some data in SQLite. This is the code to edit the data:
db.open();
c = db.getData();
if (c.moveToFirst()) {
do {
Log.v("_______BACK PRESSED", "______UUID___________"+c.getString(c.getColumnIndex("uuid")));
Log.v("_______pos", "______UUID___________"+pos);
String strSQL = "UPDATE DeviceDetails SET devicename ="+ edittext.getText().toString() +" WHERE uuid = "+c.getString(c.getColumnIndex("uuid")) ;
db.select(strSQL);
Log.v("_______BACK PRESSED", "______UUID___________"+c.getString(c.getColumnIndex("uuid")));
Log.v("_______BACK PRESSED", "______devicename___________"+c.getString(c.getColumnIndex("devicename")));
Log.v("___________text", "_______________"+c.getString(c.getColumnIndex("devicename")));
Log.v("___________edittext", "_______________"+edittext.getText().toString());
Log.v("_____ADDRESS______edittext", "_______________"+pos);
Intent intent=new Intent();
intent.putExtra("Dname", edittext.getText().toString());
Log.v("_____edittext in intent________", "__________"+edittext.getText().toString());
intent.putExtra("Daddress",pos);
Log.v("_____edittext in intent________", "__________"+pos);
setResult(RESULT_OK, intent);
finish();
} while (c.moveToNext());
}
db.close();
and in database adapter, I did as following for executing the query. But it is not updating the data.
public Cursor select(String query) throws SQLException {
return db.rawQuery(query, null);
}
Your string literals are not properly '' quoted for SQL. However, the best option is to use ? placeholder and bind arguments.
Use execSQL() and not rawQuery() for updates. rawQuery() alone won't run the SQL; execSQL() will.
Example:
db.execSQL("UPDATE DeviceDetails SET devicename = ? WHERE uuid = ?",
new String[] {
edittext.getText().toString(),
c.getString(c.getColumnIndex("uuid"))
});
What do you mean by renaming its simple updating
and an update query will do this as you are doing so
just update your query with this
String strSQL = "UPDATE DeviceDetails SET devicename ='"+ edittext.getText().toString() +"' WHERE uuid = '"+c.getString(c.getColumnIndex("uuid"))+"'" ;
You should use .update to update rows in your database not .rawQuery
String value = c.getString(c.getColumnIndex("uuid"));
String[] whereVars = new String[]{value};
String where = "uuid = ?"
ContentValues args = new ContentValues();
args.put("devicename", edittext.getText().toString());
db.update("DeviceDetails", args, where, whereVars);
I'll also let you know that the performance for your above code, updates in a loop, will be crap.. so you should do all updates in a single transaction:
db.beginTransaction();
try {
if (c.moveToFirst()) {
do {
updateDeviceByUuid(c.getString(c.getColumnIndex("uuid"),
edittext.getText().toString());
} while (c.moveToNext());
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
and
public int updateDeviceByUuid(String uuid, String deviceName){
String[] whereVars = new String[]{uuid};
String where = "uuid = ?"
ContentValues args = new ContentValues();
args.put("devicename", deviceName);
return db.update("DeviceDetails", args, where, whereVars);
}
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 });
}
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;
}
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);