I am making a mobile application which scans the device for audio files and I am putting these files in a sqlite db.
Now, my strategy is I commit first 100 entries to the db and make them available for browsing and play and then when I have scanned the rest of the files , I commit the rest thousands of files at the end, thus completing the db.
But I found that after my first commit of 100 files, sqlite file is not generated till I make a second commit of remaining thousands of files and close the db.
Why the db file is not generated?
Check if you are invoking proper "flush" of your output streams wherever they are needed. Also if you play with transactions all the stuff happens in memory until you do committing.
Close of course does the flushing for you.
Related
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.
I have connected my device and accessed my applications data by going to /data/data/my.package.name/databases.
Here I can see files:
data
data-shm
data-wal
As I can understand these files are specific to android system itself, but they represent SQLite database and yet they are not mountable to SQLite reader?
I have this issue when data is being downloaded and stored to database, after some time data-wal starts to become extremely huge (from maybe 12MB to 7GB) and after sync finished it becomes almost empty again. Am I correct in saying that this is probably the issue with transactions (somewhere transaction is not being closed and is always opened and because of this reason data-wal is being filled with back-up data in case of a rollback)?
The database consists of these three files. When opening data, the other two are automatically used when needed.
The -wal file contains all changes made to the database since the last checkpoint. To prevent the -wal file from becoming too large, use smaller transactions so that SQLite is able to do checkpoints, or just disable WAL mode altogether during bulk writes.
In my application that i wrote using sqlite for data persistence.
I decide to create local db file and to this file i read/write my data.
Because there is an option that the user will do something ( like turn off his device ) that will corrupt this db file - i want to create backup db file that will be override at the end of the writing process to the db file.
( i will always hold 2 db files .. one is the not last update that differently not corrupted and one is 'dirty' db file that hold the latest data - and my app will write to this file and make the swap between those two files after writing )
Is there some other way to avoid a case of corrupted db file ?
Is there some code example or some lib of how to do this backup on android code ?
One option to avoid the risk of a local SQLite database getting corrupted would be to mirror your critical data to the server, ultimately to a more permanent database which is more stable. So as an example here you could periodically sync your local user data with the server. If you detect corrupt data, of if things are so bad that you can't read at all, you would not update with that data, and you might even use the server side database data for doing a restore of some kind.
I recommend you to check out Room - a new library which is an abstraction layer over SQLite that comes to deal with issues programmers face when managing SQLite database manually. Personally, I've migrated to Room and it solved for me some issues (other than yours, but still) that I was struggling with before.
I have made an application which is about pets, at first I had kept the database local to the phone, however as new features arrive I want to make it a network based application with remote database. However I have around 500 downloads on the play store and I dont want my previous users to lose data. I came up with an idea of rolling out an update in which I copy all the databases to the SD card and then mail them back to me and update them in the remote database. I wonder if there is a better way to go around this. Help will be highly appreciated.
A better way would be to write a webservice to which you can send the database row by row. The webservice will then update your Server database(s).
This not only prevents duplication of your database between internal and external memory, but it also allows automation and more flexibility in the update process. You can also pause the transfer, and pick up from whichever row was last sent easily.
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)