I am using below code to update database and putting where clause for date
ContentValues values = new ContentValues();
values.put(DBTransactions.USER_ID_KEY, userId);
values.put(DBTransactions.DATE_KEY, date);
values.put(DBTransactions.DAY_KEY, day);
values.put(DBTransactions.GOAL_KEY, dailyGoal);
values.put(DBTransactions.VOLUME_KEY, dailyVolume);
values.put(DBTransactions.IS_GOAL_MET_KEY, goalMet);
db.getWritableDatabase().update(DBTransactions.TRANSACTION_TABLE_NAME, values, DBTransactions.DATE_KEY + " = ?", new String[]{date});
closeDB();
am I wrong somewhere in putting where clause? Its not updating database, Its creating new entry in database.
Its not updating database.
Thanks in Advance
I am using this code for update me Database and it's work fine , not a issue with me .
public void updateItemToCartTable(String itemId, String subitemid, String cost, int quantity) {
sdb = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(ITEM_COST, cost);
values.put(ITEM_QUANTITY, quantity);
Log.d("DB", "Count : " + quantity);
this.sdb.update(TABLE_NAME, values, ITEM_ID + " ='" + itemId + "' AND " + SUB_ITEM_ID + " ='" + subitemid + "'", null);
sdb.close();
}
Use This Code For Updating your Database
Use below code to update your table:
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(DBTransactions.USER_ID_KEY, userId);
values.put(DBTransactions.DATE_KEY, date);
values.put(DBTransactions.DAY_KEY, day);
values.put(DBTransactions.GOAL_KEY, dailyGoal);
values.put(DBTransactions.VOLUME_KEY, dailyVolume);
values.put(DBTransactions.IS_GOAL_MET_KEY, goalMet);
db.update(DBTransactions.TRANSACTION_TABLE_NAME, values, DBTransactions.DATE_KEY + " = ? ", new String[]{String.valueOf(date)});
closeDB();
What is data type of DBTransactions.DATE_KEY column in DBTransactions.TRANSACTION_TABLE_NAME table?
Insert data in local database and check the format in which date is inserting in DBTransactions.TRANSACTION_TABLE_NAME table.
While updating record based on DBTransactions.DATE_KEY condition, check DBTransactions.DATE_KEY matches value stored in DBTransactions.TRANSACTION_TABLE_NAME DBTransactions.DATE_KEY column.
Everything else in your code seems to be fine.
Related
I want to swap sqlite data
now, I am changing data after saving it in a variable
Is there a better way? Thank you
updateDbDate(fromItem.getTitle(),fromItem.getCable(),fromItem.getImg(),toItem.getId());
updateDbDate(toItem.getTitle(),toItem.getCable(), toItem.getImg(),fromItem.getId());
public void updateDbDate(String mTitle, String mCable, String mImg, int id)
{
ContentValues values = new ContentValues();
values.put("title", mTitle);
values.put("cable", mCable);
values.put("bimg", mImg);
db.update(Define.DB, values,"_id = " + "'" + id + "'",null);
}
What I understand is that you want to swap the values of 3 columns between 2 rows for which you know the ids.
If you also know the values of the other 3 columns then your method is correct.
If you don't know them and you have to read them from the table then I think it would be better to just swap the ids:
public void swapIds(int id1, int id2) {
ContentValues values = new ContentValues();
values.put("_id", -1);
db.update(Define.DB, values, "_id = ?", new String[] {String.valueOf(id1)});
values.put("_id", id1);
db.update(Define.DB, values, "_id = ?", new String[] {String.valueOf(id2)});
values.put("_id", id2);
db.update(Define.DB, values, "_id = ?", new String[] {"-1"});
db.close();
}
The 1st update sets temporarily -1 to the first _id, because I guess it is the PRIMARY KEY and it can't be set to id2 because it already exists.
The 2nd update sets id1 to the second _id (it can be done since id1 does not exist in the table from the previous update).
And the 3d update sets id2 to the first _id.
Now you can call the method:
swapIds(100, 200);
Note: this method is not recommended if there are references in other tables to the _id of this table.
I am playing with an example of SQLite I found on the internet. I have an update statement like this:
public int updateContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, contact.getName());
return db.update(TABLE_CONTACTS, values, KEY_ID + " = ?" +
contact.getID(), null);
}
And an update statement like this:
public int updateContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, contact.getName());
return db.update(TABLE_CONTACTS, values, KEY_ID + " = ?",
new String[]{String.valueOf(contact.getID())});
}
Can someone tell me the difference?
Here is the method signature for an update operation on SQLite database.
int android.database.sqlite.SQLiteDatabase.update(
String table, ContentValues values, String whereClause, String[] whereArgs)
From developer.android.com
You may include ?s in the where clause, which will be replaced
by the values from whereArgs. The values will be bound as Strings.
Btw your first example wouldn't work cause you have included
"KEY_ID + " = ?" + contact.getID()" in whereClause param and kept the whereArgs null. The ?s would'nt be replaced by your arg contact.getId()
Change whereClause in 1st example to this: KEY_ID + " = " + contact.getID()
On your 1st example.The code can't update anything.
The update method whereCause params will be convert to some SQL on where case ,It will replace the ? placeholder with whereArgs.
Such as:
In your 1st example.If contact.getId() return 1,The final SQL is like:
update contact set KEY_NAME = 'your contact name ' where KEY_ID = ? 1
but 2st example final SQL is like:
update contact set KEY_NAME = 'your contact name ' where KEY_ID = 1
So,the first example is not work.
I was trying to duplicate this SQLite statement from the line of code below:
Cursor cursor = db.rawQuery("update tbl_details SET ticket = replace(ticket, " + tempID + ", " + ticket + ")", null);
to this one:
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("ticket", "replace(ticket, " + tempID + ", " + ticket + ")");
db.update("tbl_details", cv, null, null);
return true;
What I am trying to do is to get a New ID and replace all instances of the old temporary ID in the database. But the code above is changing all the records in ticket column.
Please help. Thank you!
You can use ContentValues to bind literal values only, not expressions like replace(...).
To run the raw UPDATE SQL, just use execSQL() instead of rawQuery(). rawQuery() alone won't actually run the code until the returned Cursor is moved.
Is there an easier way I can insert data in a table in SQLite? What I need is to insert State_ID and State_Name in Table tblState. Since there are a lot of State in US, is there another way I can use instead of this:
String ROW1 = "INSERT INTO tblState VALUES (1,'Alabama')";
String ROW2 = "INSERT INTO tblState VALUES (2,'Alaska')";
String ROW3 = "INSERT INTO tblState VALUES (3,'California')";
db.execSQL(ROW1);
db.execSQL(ROW2);
db.execSQL(ROW3);
Thanks!
Try for this..
String state[] = { "state1" , "state2",.............};
int id=1;
for(int i=0;i<state.length;i++)
{
db.execSQL("INSERT INTO tblState VALUES (id,state[i])");
id++;
}
You can do the following:
String ALLROWS = "INSERT INTO tblState"
+ "SELECT 1 AS 'State_ID', 'Alabama' AS 'State_Name'"
+ "UNION SELECT 2 AS 'State_ID', 'Alaska' AS 'State_Name'"
+ "UNION SELECT 3 AS 'State_ID', 'California' AS 'State_Name'";
db.execSQL(ALLROWS);
You should use ContentValues for each of the rows you want to insert:
ContentValues values = new ContentValues();
values.put('State_ID', 1);
values.put('State_Name', "Alabama");
and then use db.insert("tblState", null, values) to insert the row. You can write a method to construct your ContentValues:
public ContentValues method(int id, String name) {
ContentValues values = new ContentValues();
values.put('State_ID', id);
values.put('State_Name', name);
}
Your code will be:
db.insert("tblState", null, method(1, "Alabama"));
db.insert("tblState", null, method(2, "Alaska"));
db.insert("tblState", null, method(3, "California"));
Or just loop through all the states...
I'm currently developing an Android app. The database has 1 column only named content. Here it is the code:
public long insert(String content){
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_CONTENT, content);
return sqLiteDatabase.insert(MYDATABASE_TABLE, null, contentValues);
}
How to make an update for a specific column (according to its index) in that database?
db.update
(
MYDATABASE_TABLE, contentValues , ID_FIELD_CONST + " = ?",
new String[] { idValue }
);
You can use:
ContentValues cv = new ContentValues();
cv.put(KEY1,value1);
cv.put(KEY2,value2);
//... all the fields you want to update
String where = KEY_CONTENT + " = " + value;
db.update(table_name,cv,where,null);
KEY1,KEY2 ... are the name of the fields you want to update
value1,value2... are the new values of the fields you want to update
the where String is to tell in which row you need to update.