Here is my piece of code which runs without errors, but doesn't update database:
ContentValues cv_lio = new ContentValues();
cv_lio.put("u_lio",1);
String where = "u_name" + "=" + 5;
db.update(table_name, cv_lio,where,null);
Can anyone improve my update query to work well.
Proceed as follow :
ContentValues cv_lio = new ContentValues();
cv_lio.put("u_lio",1);
String where = "u_name" + " = ?";
String[] whereArgs = new String[] {String.valueOf(5)};
db.update(table_name, cv_lio, where, whereArgs);
please refer to documentation which is, I must confess, not so clear on that precise point...
The error was at where clause corrected code can be
ContentValues cv_lio = new ContentValues();
cv_lio.put("u_lio",1);
String where = "u_name='" + un + "'";
db.update(table_name, cv_lio,where,null);
Look for Quotes when you want to show field value like the one shown above..
Related
I have the table(shown in fig). I want to update the time column. I have the medicine_id(second column) how to update the different times depend upon the same medicine_id(second column).
Also, I have four rows in my DB when I updated time it should be two row means, I want to remove the other two rows with the particular medicine_id..
I have the following code for update:
if(mon) {
for (int i = 0; i < dosage_per_day; i++) {
mMedicineDbHelper.updateMedicines(new
MedicineTime(row_id, time_InMillies[i]));
}
}
My DB code:
public void updateMedicines(MedicineTime medicineTime) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(ID, medicineTime.getId());
values.put(MEDICINEID, medicineTime.getMedicine_id());
values.put(TIME, medicineTime.getTime_in_millis());
db.update(TABLENAME, values, MEDICINEID + " = ?",
new String[]{String.valueOf(medicineTime.getMedicine_id())});
db.close();
}
It shows exception..
anybody help to solve this...
For the original question asked you're executing the following code:
db.update(TABLENAME, values, MEDICINEID + " = ?",
new String[]{String.valueOf(medicineTime.getMedicine_id())});
For some reason on Android, converting numeric values to Strings and using them for selection args never works properly. See: Android Sqlite selection args[] with int values. The best way to do this is the following:
db.update(TABLENAME, values, MEDICINEID + " = " + medicineTime.getMedicine_id(), null);
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.
Is there an easy way to update a table in sqlite in android? (like a single line in built method) ? I have a table with few columns and primary is one column. I want to search by the primary key and then update a row in the table.
To use with predefined update method from android, use it as below:
ContentValues args = new ContentValues();
args.put("col_name", "new value");
db.update("table_name", args, String.format("%s = ?", "primary_column"),
new String[]{"primary_id"});
Or to run as a single line, go with this (not recommended):
db.execSQL("UPDATE table_name SET col_name='new_value' WHERE
primary_column='primary_id'");
Read the documentation for SQLiteDatabase.update
You should end up with something like this:
affected = db.update(TABLE_NAME, values, where, whereArgs);
UDPATE
Avoid raw queries using error-prone syntax at all costs. I see a lot of answers here that use a lot of '"' + SOMETHING + "'" ... this is extremely bad practice and you will spend all your time looking for errors on places that are hard to find or simply a complete waste of time.
If you must use raw queries, try forming them with String.format to avoid perilous debug sessions and migraines.
You can use rawQuery like this:
cur = mDb.rawQuery("update " + TABLE_NAME
+ " set column1=mango where id='" + _id + "'",null);
where
cur is Cursor object
TABLE_NAME is NAME OF THE TABLE
_id is name of the column (only example)
Then you should already know what's your primary key.
dbHelper.getWritableDatabase();
ContentValues values = createContentValues(profileVo);
db.update(ProfileVO.TABLE_NAME, values, ProfileVO.COLUMN_ID + "=" + profile.getId(), null)
Here's a good tutorial for you http://www.vogella.com/articles/AndroidSQLite/article.html
The answer is:
http://www.sqlite.org/lang_update.html
and
SQLiteDatabase.rawQuery(...)
Try this:
public void updateFunction(int id) {
String updateStmnt = "UPDATE YOUR_TABLE SET YOUR_COLUMN = "
+ id;
database.execSQL(updateStmnt);
}
Hope it will help.
Using database.update make it simple like this:
ContentValues values = new ContentValues();
values.put(MySQLiteHelper.COLUMN_NAME, name);
values.put(MySQLiteHelper.COLUMN_JOB, job);
values.put(MySQLiteHelper.COLUMN_DATE_START, date_start);
database.update(MySQLiteHelper.TABLE_EMPLOYEES, values, MySQLiteHelper.COLUMN_ID+"="+id, null);
I know this a bit old, but in case anyone needed another way:
public boolean updateNote(Note note) {
SQLiteDatabase db = notesDbHelper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(NotesDbContract.NoteEntry._ID, note.getId());
contentValues.put(NotesDbContract.NoteEntry.COLUMN_NAME_TITLE, note.getTitle());
contentValues.put(NotesDbContract.NoteEntry.COLUMN_NAME_DSECRIPTION, note.getDescription());
int result = db.update(NotesDbContract.NoteEntry.TABLE_NAME,
contentValues,
NotesDbContract.NoteEntry._ID + "=?", new String[]{String.valueOf(note.getId())}
);
db.close();
return result > 0;
}
I am trying to update the database to show that something has been uploaded. At the end of the method where I have uploaded the information I have the following code to change a column value from 0 to 1 where 1 means that something has been uploaded but I am getting an error.
Here is what my code looks like:
ContentValues cv = new ContentValues();
cv.put(ProviderMetaData.TableMetaData.HAS_UPLOADED, "1");
String where = ProviderMetaData.TableMetaData._ID + "=" + contenturi;
String[] value = {"0"};
ContentResolver cr = this.mContext.getContentResolver();
cr.update(ProviderMetaData.TableMetaData.CONTENT_URI, cv, where, value);
and the error message I am getting is as follows:
Error upldating hasupladed=1 using UPDATE tablename SET hasuploaded=? WHERE _id=1
Does anyone see the error here or know what I am doing wrong?
I am not sure of what is the value of the variable contenturi, but you do not need it there. If I am not wrong the correct code you need is either:
ContentValues cv = new ContentValues();
cv.put(ProviderMetaData.TableMetaData.HAS_UPLOADED, "1");
String where = ProviderMetaData.TableMetaData._ID + "= ?";
String[] value = {"0"};
ContentResolver cr = this.mContext.getContentResolver();
cr.update(ProviderMetaData.TableMetaData.CONTENT_URI, cv, where, value);
Or even:
ContentValues cv = new ContentValues();
cv.put(ProviderMetaData.TableMetaData.HAS_UPLOADED, "1");
String where = ProviderMetaData.TableMetaData._ID + "= ?";
String[] value = {String.valueOf(contenturi)};
ContentResolver cr = this.mContext.getContentResolver();
cr.update(ProviderMetaData.TableMetaData.CONTENT_URI, cv, where, value);
Depending on whether you want the id set dynamically or not.
The main difference between my code and yours is that I use the where arguments as they are meant to be used - in the where caluse you place question marks on the places where their values are to be inserted and then Android SQLiteDatabase substitutes their values accordingly.
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.