SQLite - Updating multiple tables - android

Can anyone help me fixing the problem in my query?
I'm trying to call a function to update multiple tables which have a value of "1" in column "col", but it's not working,
my_function
public long updateNow() {
mDbHelper = new DatabaseHelper(mContext);
mDb = mDbHelper.getWritableDatabase();
int doneClear = 0;
String base_value = "1"; // update all column "col" where value of 1
ContentValues initialValues = new ContentValues();
initialValues.put("col", "0"); // Update all column "col" with 0
doneClear = mDb.update(SQLITE_TABLE_ONE, initialValues, "col=" + base_value, null);
doneClear = mDb.update(SQLITE_TABLE_TWO, initialValues, "col=" + base_value, null);
doneClear = mDb.update(SQLITE_TABLE_THREE, initialValues, "col=" + base_value, null);
doneClear = mDb.update(SQLITE_TABLE_FOUR, initialValues, "col=" + base_value, null);
doneClear = mDb.update(SQLITE_TABLE_FIVE, initialValues, "col=" + base_value, null);
doneClear = mDb.update(SQLITE_TABLE_SIX, initialValues, "col=" + base_value, null);
Log.w(TAG, Integer.toString(doneClear));
return doneClear;
}
This code is not working, i'm can't get why.
Nothing happens in column col.
Anyway, that code is based on my previous query, which is working fine.
working_update_query
public long updates(String recent_value, String _id, String table_name) {
ContentValues initialValues = new ContentValues();
initialValues.put("recent_value", recent_value);
Log.w(TAG, String.valueOf(initialValues) + " WITH ID OF " + _id + " IN TABLE OF " + table_name);
return mDb.update(table_name, initialValues, "_id=" + _id, null);
}
I'm stuck here for almost 1 hour, can't figure out the problem.
Can anyone help me please?

Try this using wildcards to appropriately inject the update parameters, like this:
int updateCount = db.update(
TABLE_NAME,
newValues,
"col = ?",
new String[] {String.valueOf(colValue)}
);
For a quick sample check out this tutorial.

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 - How to update and delete only one row in SQLite Database using where arguments?

So I am trying to delete only one instance of values from my SQLite database using the where args of db.update and db.delete in my app but it seems to delete and update all the matching ones. I only want to update or delete one. How do I do it? I tried using cursors but my implementation seems wrong. Help?
public long updateData(String oldFood, String oldCalorie, String newFood, Float newCalorie, String newDate) {
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(helper.FOOD_COLUMN, newFood);
contentValues.put(helper.CALORIE_COLUMN, newCalorie);
contentValues.put(helper.DATE, newDate);
String[] columns = {helper.UID, helper.FOOD_COLUMN, helper.CALORIE_COLUMN, helper.DATE};
//long id = db.update(helper.TABLE_NAME, contentValues, helper.FOOD_COLUMN + " = ? AND " + helper.CALORIE_COLUMN + " = ?", new String[]{oldFood, oldCalorie});
Cursor cursor = db.query(helper.TABLE_NAME, columns, null, null, null, null, null);
Information current = new Information();
int uidIndex = cursor.getColumnIndex(helper.UID);
int cid = cursor.getInt(uidIndex);
int foodIndex = cursor.getColumnIndex(helper.FOOD_COLUMN);
int calorieIndex = cursor.getColumnIndex(helper.CALORIE_COLUMN);
float oldCalorieFloat = Float.parseFloat(oldCalorie);
if (oldFood == cursor.getString(foodIndex) && oldCalorieFloat == cursor.getFloat(calorieIndex)) {
long id = db.update(helper.TABLE_NAME, contentValues, helper.FOOD_COLUMN + " = ? AND " + helper.CALORIE_COLUMN + " = ?", new String[]{oldFood, oldCalorie});
db.close();
return id;
}
long id=0;
return id;
}

SQLite database update query with multiple where conditions in Android

I want to update my database table with multiple where conditions. I already did with single where condition
db.update(TABLE_MISSING_ITEMS, values, KEY_AUTHOR + " = ?",
new String[] { String.valueOf(items.getAuthor()) });
Now i want 2 where condition.
P.S :- No raw query
You can separate the different WHERE conditions with ANDlike this:
db.update(TABLE_NAME,
contentValues,
NAME + " = ? AND " + LASTNAME + " = ?",
new String[]{"Manas", "Bajaj"});
The way I solved my need
public boolean checkHususiKayit(String baslik, String tarih) {
boolean varMi = false;
SQLiteDatabase database = dbHelper.getReadableDatabase();
final String kolonlar[] = {DBHelper.COLUMN_H_ID,
DBHelper.COLUMN_H_ID,
DBHelper.COLUMN_H_BASLIK,
DBHelper.COLUMN_H_TARIH,
DBHelper.COLUMN_H_YOK_TUR,
DBHelper.COLUMN_H_AD,
DBHelper.COLUMN_H_WEB_ID,
DBHelper.COLUMN_H_NUMARA,
DBHelper.COLUMN_H_YURD_ID,
DBHelper.COLUMN_H_YETKILI_AD,
DBHelper.COLUMN_H_YETKILI_ID,
DBHelper.COLUMN_H_TEL,
DBHelper.COLUMN_H_EMAIL,
DBHelper.COLUMN_H_ADDRESS,
DBHelper.COLUMN_H_VAR,
DBHelper.COLUMN_H_GOREVLI,
DBHelper.COLUMN_H_YOK,
DBHelper.COLUMN_H_IZINLI,
DBHelper.COLUMN_H_HATIMDE};
String whereClause = DBHelper.COLUMN_H_BASLIK + " = ? AND " + DBHelper.COLUMN_H_TARIH + " = ?"; // HERE ARE OUR CONDITONS STARTS
String[] whereArgs = {baslik, tarih};
Cursor cursor = database.query(DBHelper.TABLE_NAME_HUSUSI, kolonlar, whereClause, whereArgs, null, null, null + " ASC");
while (cursor.moveToNext()) {
varMi = true;
}
database.close();
cursor.close();
return varMi;
}
THIS CAN ALSO BE DONE LIKE THIS
public void UpdateData(int Cid,int flag,String username,String password)
{
SQLiteDatabase database = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("Status",flag);//I am updating flag here
database.update(TABLE_NAME, cv, ""+KEY_UserName+"= '"+ username+"' AND "+KEY_CID+"='"+Cid+"' AND "+KEY_Password+"='"+password+"'" , null);
database.close();
}
Try this simple query
db.update(TABLE_FF_CHECKLIST_DATA,contentValues, "FFCHECKLISTID = ? and TASK_ID_CHK = ?" , new String[] {"EHS" , "CTO914"});
where "EHS" is value in column FFCHECKLISTID and CTO914 is value in TASK_ID_CHK.

SQLite Today_date - date

Good day!
I've got a problem of counting the date. I have one date (the number of a day in the year) and today's date. I need to get the result of subtraction (Today_date - COLUMN_DATE = COLUMN_LAST)
Here's my code. Obviously, it contains some mistakes.
Calendar localCalendar = Calendar.getInstance(TimeZone.getDefault());
Cursor cursor = mDB.query(DB_TABLE, new String[] {COLUMN_DATE} , null, null, null, null, null);
while(cursor.moveToNext())
{
ContentValues cv = new ContentValues();
cv.put(COLUMN_LAST, localCalendar.get(Calendar.DAY_OF_YEAR) - cursor.getInt( cursor.getColumnIndex(COLUMN_DATE) ));
mDB.update(DB_TABLE, cv, COLUMN_ID + " = " + cursor.getPosition(), null);
}
What should I do this thing to work? Thank's for your answers.
You're assuming that cursor position is equal to row id, it may not be true. When you're updating row you should pass column_id not cursor position.
Calendar localCalendar = Calendar.getInstance(TimeZone.getDefault());
Cursor cursor = mDB.query(DB_TABLE, new String[] {COLUMN_ID, COLUMN_DATE} , null, null, null, null, null);
while(cursor.moveToNext())
{
ContentValues cv = new ContentValues();
int columnId = cursor.getInt(0); // cause COLUMN_ID is 0th in projection above
cv.put(COLUMN_LAST, localCalendar.get(Calendar.DAY_OF_YEAR) - cursor.getInt(1));
mDB.update(DB_TABLE, cv, COLUMN_ID + " = ?" + id, new String[] {columnId});
}
// always close cursor
cursor.close(); // you can use try-finally block

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;
}

Categories

Resources