Android: SqliteDatabase's lastModified() is giving wrong value - android

I am using sqlcipher database. I am tracking the lastModified time of my database. According to my understanding long value returned by lastModified() function will change only if we update or add a value to the database we refer. I am using a query to fetch (not modifying) a value from the database, for this i am using the below code
mDatabaseFileObj = mContext.getDatabasePath("xxx.db");
Log.i(""," "+mDatabaseFileObj.lastModified());
mSQLiteDatabase = net.sqlcipher.database.SQLiteDatabase.openOrCreateDatabase(...)
Log.i(""," "+mDatabaseFileObj.lastModified());
mCursor = mSQLiteDatabase.rawQuery(query, null);
do{
....
}while(..)
In this i had printed two logs. First log before creation of mSQLiteDatabase obj and another log after that.According to the doc for lastModified() both the values printed by the logs should be same as i just quering not modifying the database. But the value is changing.
I couldnt sort out this problem.Give your thoughts on this.
An addtional info is, i had placed this code snippet in a function and i am calling that function 5 times and strangely for the first time alone the log is printing different values but for the rest 4 times the log printed values are same..
Thanks in Advance

Deepak,
openOrCreateDatabase is not a read only operation. In particular the wrapping library, which is based on the Android sqlite library, manipulates a table called android_metadata when the database is open. This could cause the timestamp to change, because the database is actually modified during open.

mDatabaseFileObj this is reference to your File object from OS don't confuse this with database in SQLITE database are implemented on top of file system only, so in first line you are printing when this file was last modified,
while second line you are trying to alter the file, and third line again printing time, so as per me and going with file systemn behaviour you will get a different time stamp, this doesn't mean if content inside this file was modified or not.
Just imagine it like this, open a txt file in windows and save it again without changing it notice time before and after they will be different.
Hope this help.

Related

How to retrieve data from SQLite table with inserted values

I have dozens of data value that already inserted inside Database class that extends SQLiteOpenHelper, however I want to get the data values from it and not just to view but use the values, to calculate them. I've been reading lots of examples and following tutorials, but none of them ever explain how to get a data or some data and make a use of it (in my case, use them as java variables(get from inserted data then send to another class)). To take few (not all at once), select 1 or couple to be a multiplier for user input. My app will calculate pmt function, which the user will input the price and etc, then when the user press calculate the app (will do in background) access data from database class (the value that have been inserted) get them, send them via intent to another class, then calculate the database values along with user input.
In short, the app have stored values inside its database that will then be called whenever a button is triggered. Passing the existing values to another class.
I've tried this far My Previous post, click here
There I was trying to send a data (testing) from database to a class(that should have retrieved but it got null values)
The question:
How to get a data(datas) from a database which have inserted values to another class and make a use of it as some calculate-able values?
UPDATE:
I found out that the cursor getting null values, any ideas why?
thanks in advance.
Solution has been added on the above link, check on the answer.

SQLite Database lock on large query of INSERTS OR REPLACE

I hope some one can help me with this, I currently have an android application which has an SQLite database which can execute a txt with an SQLite query for amending the database, my issue is when I use a large number of INSERT OR REPLACE I get an error DATABASE LOCKED AT LINE xxx (ever time I execute the same query its always a different line) This error seems to start occurring around 1600 Lines or more. Query currently is constructed like this for each line
INSERT OR REPLACE INTO "TABLE003" VALUES('000001','Group1',NULL,'20140519163202','20140519163202');
I currently have 2700 line which I wish to execute, If I break the query down in to about 500 lines each I don't have an issue, is this a known is on large query of INSERT OR REPLACE? and is there a work around (other than breaking the query down in to separate query of 500 lines each)?

Last object not saved after table was truncated in ActiveAndroid

I use ActiveAndroid to save my objects to the database, it works mostly well. In my application, I use the following scenario:
I save a new object to a table in my database
I select some objects from that table
I add them to a List<>
I delete everything from that table
I use foreach on my List and call 'save' on each object
And here comes the problem. In my table the objects are saved except the aforementioned most recently saved one. I created a counter to check, how many 'save' was called: the counter is 1 more than the count of the objects in the table. I debugged it, no exception was raised, the save was called. I use the latest version of ActiveAndroid (3.0.99)
Any ideas what I should check?
Well, the problem can be seen in the scenario if your read it through.
I copy an existing object to the memory and try to reinsert it. The ORM checks only the mID of the object and if it is not null, it calls an update. As my object had an id, it was tried to be updated though the table was truncated so nothing was updated.
I don't know if it is intentional that the model never checks the table just its own id but it can lead to issues like this.

Problem while updating table in android

I created a table in the database that has the data like this:
Now i have written a query that updates the contact field by concatinating name and email fields:
UPDATE MyContacts SET contact=(SELECT name||'--'||email FROM MyContacts);
Here the problem is after executing the query the table is as below:
Why is it happening like this? In oracle i never faced this problem. Please help me. Thank you
Right now you're not specifying the correct row to retrieve the values from. Try something like this:
UPDATE MyContacts SET contact = name||'--'||email;
EDIT: Glad it worked. Your first issue was that your sub-select uses a SELECT statement with no WHERE clause (SELECT name||'--'||email FROM MyContacts will return 3 rows). One possible solution would be for SQLite to throw an error and say You've tried to set a column to the result of an expression that returns more than 1 row: I've seen this with MySQL and SQL Server. However, in this case SQLite appears to just use only the very first value returned. However, your second error then kicks in: since you don't narrow your UPDATE statement with a WHERE clause, it uses that first value returned to update EVERY single row, which is what you see.

Android Widgets: Where would the 'insert' step for a database occur?

I have a widget that currently takes a random string from an array and sets it to text view on update. The issue here is that the same item can be re-used multiple times in a row due to the string being 'random'
In order to solve this I was going to create a table that held String text, and int viewednum and increment the viewed number each time 'get text' was called. (on update in the widget).
My Question: If I put the insert statements in the widget, won't the data be inserted every time 'on update' is called?
Would it be better for it to go in the DBadapter class somewhere? I'm just unsure about the best way to make sure I don't enter duplicate data. If there is a better alternative like saving a csv file somewhere and using that I'm open to it, it seemed like a sqlite database was the way to go.
Thank you for your time.
That depends on what your onUpdate method does. If each time onUpdate is called it gets a random String from the database, then that would be the place to put it. However, if you are not getting the String during onUpdate, then you should put it in the method where you are accessing your database. I think your confusion is about the purpose of onUpdate. onUpdate doesn't get called every time the user scrolls by the homepage and sees your widget; it gets called regularly on a timescale you specify, and the whole purpose of it is, in a case like yours, to get a new String from the database.
As for your second question, yes, SQlite databases are the way to do it :) I haven't tried saving a csv file or something like that, but I imagine that would be a lot more complex than just using a database.
Declare your database with a UNIQUE constraint on the columns you want to keep unique, then set the desired behaviour via ON CONFLICT in the INSERT statement. ON CONFLICT REPLACE... means the most recent INSERT overwrites. ON CONFLICT IGNORE... keeps the older version.

Categories

Resources