Clearing Application database - android

I have an Android application which uses sqlite database to store textual data for a particular user. The data is read/written from/to the database from a service that runs periodically after every n seconds. My requirement is to clear data depending on age. Like, if the file is more than 24 hours old it should be deleted.
Checking the age of the file seems easy, just compare current time with the File creation time. The problem is:
where should I put this check and delete the file; inside application onCreate() or when the user logs in/ logs out? What would be an ideal place to trigger this logic from? Should I schedule an Alarm when the user logs in?
Should I delete the file or simply delete the rows? If I don't trigger the Cache clearance logic from login/logout, won't deleting the file cause problems, especially if the service is still trying to read and write from the database?
Please advice.
Thanks.

Well, this all depends on your logic for the application for the second part. I cant see why you would delete a database unless its just used to store temp data that does not matter. Either way the ideal place to do this check and delete is in the Data Access class thats opening the connection to the database. Below would be my logic...
Call to open DB
Check if DB file is old
If yes, delete it
Open Database (should create one if it does not exist)

Related

Delete images and pdf when the session is over

I want to develop a healthcare app in android. the doctor will be authenticate for a specific time to access patient's medical reports and download them to the application (reports will be in a block-chain or a db). when the session is over all of those downloaded data (reports) should be permanently deleted from the doctors mobile. what is the best approach to delete these data?
Storing files in DB is never advised. Rather, they should be stored as File themselves and you can save their path in the DB searching and accessing the files.
Your point about session timeout is too broad. It could be carried out in several ways, like Logout, Time Limit expired, Case closed from the Patient/Doctor's End etc.
You can try these steps if you find them suitable:
Once the doctor selects documents to be saved, download and save them in the Internal Storage of your app. Concurrently, save their respective path and download timestamp in a DB Table for future reference.
If your files are confidential and shouldn't be read outside your app, you can either encrypt them using an encryption algorithm and then save them on the device. You can also save them in different extensions and with random names to further make it complex for general users to extract them from the device. You will have to decrypt them at the time of viewing though.
If you think that the data in the file can be parsed and raw (text) data can be extracted, you can also try implementing a DB table and save such information in the DB itself. In such a case, there would be no files being saved on the device.
Now, you have your content (be it in a file system or DB) and your next task is to delete them once the session is over.
For LogOut Case, simply delete all the available data (both from the file system and DB), cleaning everything.
For Doctor Deleting the case, You can remove all the files for the selected case from the device. This information could be easily maintained in a DB Table.
For the case where a patient deletes/closes, you will have to implement a Push Notification service, wherein your server will send a delete command to the device. On receiving the notification on the app, you can follow the same steps.
For time limit expired, The simplest logic is to check, either every day at a certain time or every time your app is opened, for all the files which have a timestamp having 7 days older than today's date. Note the timestamp and file information is stored in the DB.
To check every day at a certain time, You will have to implement AlarmManager which will invoke a background service to carry out the task.
Note: There could be more possible ways to do such a specific task, however, these are the simplest and most widely used approaches.
Well, when the doctor will be authenticated you should start some type of a timer (for how long will he be authenticated to use patient's records) and save the paths to these files in DB. After the timer hits the 0 or the max value, you should have a listener or observer that just deletes the files from his mobile (using the paths saved in the database). You can delete files using File class.
Well, the best approach would be to create a cache directory with some unique name that distinguishes all the patients' records and caching all the downloadable items into that directory and deleting that directory upon completion of the session.

How do we know when our application data get cleared in android?

I am working on some application, where I am storing some of my application data in SQLite DB. If any user clears the data, my data gets cleared. Through How to disable the Clear Data button in Application info of Manage appliaction and http://groups.google.com/group/android-developers/browse_thread/thread/b1091f190f7356b7 these links, I found that we cannot disable clear data button, But my question is Is there any way to catch this action(clear data) in the application? Is there any intent in android to notify this action ?
Thanks in advance
Pushpa
A possible workaround is to program your application to use a SQLite database on external storage. SD card etc. just create a folder on your SD or whatever storage you want, place the .db file in that folder and use that instead of the default storage location. By default, SQLite databases are stored in the data folder(device not rooted so dont know where in the data folder), which is not accesible unless you rooted your device.
There are tutorials out there that use this technique.
Note that, its likely that when you delete your app, the data will persist.
Don't know if that is desirable.
You can do one thing You can create a test table in your DB and insert some data in it. Every time you run the application you check the value of the table exist or not. If data exist that means DB value is not reset and if Table is empty you get to know that DB data is cleared and insert the data again for the next time check.

One time insert, multiple reads into SQLite Android app

I know there are similar responses so I am going to make this very succinct. I am planning on developing an app which has 18 chapters and each chapter has 30 or 40 hymns. Now, Im planning on using an SQLite command, insert each hymn individually but after the insert, and after the APK file is generated, would the data on the database still be present? Or Does it need to inserted in on each install? What are my options?
If you use sqlite database for you app...every time the app is installed.. new database will be created(of course the old one will be deleted).. and so the hymns will be inserted on each install..(but once you install.. on running your app wont create new database and insertions..).. hope this is clear..
I am not Clear with your question..but if you r inserting data through your code..than on each installation your records would be inserted once..if you provide condition to do so for only once.
You have to code the insertion of the hymns at the start/launch of the application, so that the database is ready for retrieval for the application. But the next time, the application is started, check whether your database exists and has the hyms (size of database), if yes, dont populate the database again, if no, populate it. I hope you want to read from a file/array and insert the records to the database.Once you insert the records to the database, they are available for the reference until the application is uninstalled or the database is re-created. Sqlite database is a persistent storage. Now, Im planning on using an SQLite command, insert each hymn individually but after the insert, and after the APK file is generated, would the data on the database still be present? Yes it would be present. Once the application is installed, the code of database would be executed and the database would be created.
My suggestion to you is use the XML parsing to show the Hymens in place of sqllite. Simply create the xml file with the hymen tag then get the tag and show the data on screen.

Using Database to call objects

I need some advice. I want to implement a set up, which has 4 static saved objects saved in a database. How would a user select one of these objects to use in a service... When using a SQLDatabase, do you have to create a database every time the app starts.
Any advice welcome.
Thanks
When the application starts for the first time the database is created.
The SQLite database saves the information you need into the database, and isnt erased until you allow the user to delete it or update the database.
You can find a very good tutorial on using a database here
EDIT:
It has means for you to allow the user to update the database also, such as information,names,numbers,ect.

How to keep stored data even when an application update is done?

My question is simple :
I have a "flag" stored in SharredPreference but when the user is doing an application update, this data is ereased. Is there a way to keep data stored even if the user is doing an application update ?
Well, as far as I know you have only two choices - write it in a file or save it in the database. I always put the data in the db, from witch they are loaded at program start. Just have a table with the prefs in it and you are sure, that your data is not deleted, even after updating. The database interface is easy to use and lean - therefor I use it.

Categories

Resources