When I try to put some condition value into ContentValues variable, there is inserting as String.
contentValues = new ContentValues();
contentValues.put("DLM", "julianday('now', 'localtime')");
After executing
count = db.update(TABLE_NAME, contentValues, selection, selectionArgs);
the field is updated but the value is incorrect. I need to have the numeric date in the field, not string.
Another problem if I need to update existing field with the calculated value:
UPDATE tbl SET field=field*2
When I put the value like
contentValues.put("field", "field*2");
It has put the value as String. How I can get the value I really needed?
I think instead of update() method you should use execSQL method which allows you completely control the query syntax. Also instead of calculating fields in query you might consider creating triggers.
you need to enter date like:
Calendar cal=Calendar.getInstance();
ContentValues values = new ContentValues();
values.put("DLM", String.format("%1$te-%1$tB-%1$tY",cal));//this will insert date with format 2 July 2011
For updating values like fieldValue*2,
you need to fetch the older values from table and then you need to pass it like:
values.put("field_name",new_field_value);
Related
How I can add text without replace data ?
usually us, update and change the value, but is there any way to preserve the value + add another?
ContentValues values = new ContentValues();
values.put(text, newText;
db.update(table, values,.... ", null);
You can't do it with db.update(). I can't give you an exact answer, since I don't know what you're trying to accomplish. If you want to keep your database schema as it is, you need to get the existing record, get the existing value from it, append the new value to the existing value, and then update the database with the resulting value.
To apend some text to all values in a column, execute an SQL command like this:
UPDATE MyTable SET Text = Text + 'new text'
The update function does not support SQL expressions; it always replaces values.
The above command must be executed with the execSQL function instead:
db.execSQL("UPDATE "+table+" SET "+text+" = "+text+" + ?",
new Object[] { newText });
Could someone suggest me a way on how to update my apps SQLite database when the content of the parsed JSON file is changed?
I am having a hard time finding the right solution, now I have to clear the cache before the app starts, but the end user obviously won't do that.
I asume changing the table version is not suitable for frequent updates (hourly).
Thanks!
As #KenWolf mentioned this documentation in the comments.
Update a Database:
When you need to modify a subset of your database values, use the update() method.
Updating the table combines the content values syntax of insert() with the where syntax of delete().
SQLiteDatabase db = mDbHelper.getWritableDatabase();
// New value for one column
ContentValues values = new ContentValues();
values.put(FeedEntry.COLUMN_NAME_TITLE, title);
// Which row to update, based on the title
String selection = FeedEntry.COLUMN_NAME_TITLE + " LIKE ?";
String[] selectionArgs = { "MyTitle" };
int count = db.update(
FeedReaderDbHelper.FeedEntry.TABLE_NAME,
values,
selection,
selectionArgs);
Change your database version by incrementing by 1 from current value.
It will make possible of updation.
I'm writing an Android app that needs to write to the SQLite database. Currently it's using rawQueryWithString to build the update query, and I'm using ? placeholders in my query combined with the selectionArgs argument to pass in the actual values.
However, sometimes I actually want to update my column (of type Date) to NULL, but if I pass in null in my selectionArgs then I get this error:
IllegalArgumentException: the bind value at index 1 is null
I can't really see how I'm supposed to make it work when the value is actually null. I guess I could pass in an empty string, which in the case of a Date column like this might just work, but suppose it was a string column and I actually did want to mean NULL in contrast to the empty string (or are they considered equivalent in SQLite?)
Here's the code:
String timestampStr = null; // Obviously not really set like this
SQLiteDatabase d = getWritableDatabase();
DBCursor c = (DBCursor) d.rawQueryWithFactory(
new DBCursor.Factory(),
"Update subject set schedulingTimestamp = ? where identifier = ?",
new String[] { timestampStr, subjId.toString() },
null);
d.close();
The column was added with the following query, so I presume it's a nullable column since I didn't specify otherwise:
ALTER TABLE subject ADD schedulingTimestamp DATE;
Wildcards are not meant to be used for inserting/updating values in SQL, AFAIK. In Android, you can use ContentValues instead in conjunction with the update() method, instead of trying to shoehorn it in the raw query method.
I've been trying to update on of my column in my database named COLUMN_NAME_PERIOD_END but it doesn't seem to be able to.
String[] endDate = new String[1];
endDate[0] = paraPeriodDetailsPojoObj.getPeriodEndDate();
Log.d("PeriodEnd.onClick()", "Date: " + endDate[0]);
/*UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value
*/
sqliteDatabase.update(DBStorage.TABLE_NAME_PERIODSTART, contentValues, DBStorage.COLUMN_NAME_PERIOD_END+"=?", endDate);
No error messages were displayed so I suppose it is my update statement that is having errors. :(
From the docs:
update(String table, ContentValues values, String whereClause, String[] whereArgs)
So, you are putting the array of contentValues (which you don't show, so the problem could be there) into the table defined by TABLE_NAME_PERIODSTART, and selecting which record to update by searching the column defined by COLUMN_NAME_PERIOD_END which must equal endDate.
The way I read your question title I'm under the impression you actually want to update the COLUMN_NAME_PERIOD_END column with the endDate... if that's the case your SQL is incorrect.
Otherwise, some possibilities that leap to mind to look for are:
1) Constraints... do you have a not null field defined and you are trying to update with a null?
2) Your date format from the program is differing from the one in the database
3) There is no matching record for the endDate you are searching for in the DB
I have to execute the following query in Android SQLite
UPDATE player SET rank=rank+1 WHERE rank >= 3;
Here rank is the column name of INTEGER type.
I know about update method
update(tablename, values, whereClause, whereArgs);
I will pass player as first argument, rank >= 3 as 3rd argument and null as last argument but I am unable to find what to pass as 2nd argument. According to the docs it should be an object of ContentValues.
so I created its object as
ContentValues values = new ContentValues();
values.put("rank",?????);
I am confused about what to put the value for rank key which is a column in my table. Please suggest me what to pass as value to put() method of values
alternatively you can use exec , and execute your sql command
db.execSQL("UPDATE player SET rank=rank+1 WHERE rank >= 3");
to do it using the update method, you will need to get the current rank in a variable using Cursor, and then do the + thing manually "current+1"
I believe update method is not One-Size-Fit-All method. So I think you need to use rawQuery() to achieve this kind of sql execution. for example:
db.rawQuery("UPDATE player SET rank=rank+1 WHERE rank >= 3;", null);
or perhaps use execSQL as said by el7r