Is there any Android Official way of doing sql db backup to SD or Phone? there are several backup helper classes mentioned in the document but all of 'em are pretty useless.
I know that the sql file is a simple db file i can use Java IO class to read and write somewhere. but that is not a wise idea if we have latest version of App which contains several newly added columns in it. we cannot restore back.
Are there anyway to overcome this issue? Any easy way that so called android engineers provided in the document?
Have you seen the Backup Manager?http://developer.android.com/guide/topics/data/backup.html
Here is an article on how to use it with Gson.
https://advancedweb.hu/2015/01/06/efficient-sqlite-backup-on-android/
Not sure if this is the best solution but this will allow you to pick and choose the columns you want to restore.
I use the following approach:
make a copy of db file while back up.
Read that backup db file while restore using normal sqlite APIs.
Perform any operations like inserting default values for new columns if required and insert the data into the current db.You can perform the operations based on the version of db..The version of db can be encoded in the file name-something like backup_version_2.2.db
delete the backup db file
Related
Since there are two ways by which we can store or copy the data from database. one is from sqlite file which we lay in assets folder and another by database which is created programmatically.
I have following questions regarding the sqlite database in android:
1.which performs faster while getting performing select query from database and why?
2.what are the advantages of each approach.
I found on internet but i could not get any proper information, the only thing that i come to know by Googling is copying data from assets is better for static data like list of country(that is obvious because you will not perform any bulk operation of inserting data programmatcially in android). Any help regarding this is appreciated . :)
It does not make a difference.
Your select query is performed on the final database, which would be identical whether you copied it from assets, or imported it programmatically. Your approach to its creation does not influence its behaviour.
As for advantages, that's entirely up to you. In some scenarios, having a base database in assets would give you better, and easier to read, control over versioning, as you could commit it easily with your repository, vs. having thousands of SQL statements in code.
On the flip side, updating a database that you copy from the assets folder with each version could be difficult, especially if you modify data within the app frequently. Instead of programmatically simply altering the schema or data on update, you would have to export any modifications, copy the new database, and then import all your modifications by performing some form of merge.
I have created a database which contains 4 tables using Firefox plugin. The format of the saved database is .sqlite(name.sqlite).
I just want to know how/where to insert it into the android project that I am developing and what the changes are that I should make in my project (like changes in the Manifest file). I guess I should paste it into an asset folder, but I do not know how to access it in the code.
I want to add, view, update and delete data in the table, how should Ido this? Or Should I use another way?
This is a pretty solid tutorial: http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/
Basically, don't copy your sqlite file directly into the project. Follow the steps and create / upgrade your database in the provided onCreate and onUpgrade hooks.
These hooks give you the opportunity to change your database structure when publishing updates through the Play Store.
And the proper places to do longer running operations to upgrade all your data, or prefill your data from disk/network.
If you want to prefill your DB from a dataset, you can provide that data in a raw XML, or JSON, or CSV, and after your onCreate do all the inserts when the app launches from a fresh install.
Here is some good tuts for sqlite...
http://coderzheaven.com/2011/04/using-sqlite-in-android-a-really-simple-example/
http://androidexample.com/SQLite_Database_Manipulation_Class_-_Android_Example/index.php?view=article_discription&aid=51
http://www.vogella.de/articles/AndroidSQLite/article.html
I'm trying to create some sort of backup & restore function in my app. Before that, I've been reading for a while to understand if it's possible to achieve, but I found out this question:
Sqlite DB Android Backup/Restore
The only other way I could see to do it, would be to read the actual contents of the DB and generate a file containing the SQL which which it can be restored from, this is obviously a more complex and doesn't offer any advantages to justify this complexity.
This answer, I think, is the best way to accomplish that; not explorting the .db file, but exporting queries.
You know; when you export a SQL data from mysql, you get a file which contains all the queries that creates the structure and queries that fill the structure with data.
That's what I'm trying to mimic; generate a file which contains sql queries from a .db file.
Do you guys think it's possible, I mean, is there any builtin method to achieve that?
Otherwise, if its too hard to handle, how do you manage to avoid what this user (https://stackoverflow.com/a/10842043/1943607) is talking about?
So, I disabled WAL with "PRAGMA journal_mode = DELETE" and then I was able to view the database in the browser and able to restore it on my test device fine.
That previous part, I can't understand it. Is this a configuration you set to sqlite?
Thanks
I haven't actually tried this with sqlite, but with mysql you could do things like create "dumps" of your database. Those dumps contained exactly what you describe: a set of queries that, when executed together, recreate the database, including the contents.
Judging from the "sqlite3" documentation found at http://www.sqlite.org/sqlite.html (especially the "Converting An Entire Database To An ASCII Text File" section), you can do the same for sqlite. Since you can execute shell commands from a java application (using Runtime.getRuntime().exec() methods), and you are the "owner" (Linux user id) of the database, you should be able to run this "sqlite3 .dump" command even on a non-rooted device. I have never seen an Android device without the sqlite3 tool installed, so the command should always be available.
Moreover, since dump file is just a text file, you should be able to prepend any PRAGMA's to it that are required for compatibility (like the one you quoted).
I haven't tested any of this, but just wanted to think with you on this interesting topic.
An sqlite database is just a file so you could copy the file but I think you may have problems with permissions in android preventing you from accessing the database.
A better solution IMO would be to sync your data to an external website.
Using a combination of a custom sync adapter and the account manager with a website or web service that has a RESTfull api to receive and send the synced data would be the most reliable approach.
http://developer.android.com/training/id-auth/identify.html is a great introduction to setting up the account manager.
And for a custom sync adapter this is a great starting point.
http://www.c99.org/2010/01/23/writing-an-android-sync-provider-part-1/
and http://www.c99.org/2010/01/23/writing-an-android-sync-provider-part-2/
And finally an explanation of how it all fits together
https://sites.google.com/site/andsamples/concept-of-syncadapter-androidcontentabstractthreadedsyncadapter
The above approach would enable a user to switch phones and retain data at the same time and the data would always be up to date (providing you sync at the appropriate times.
It seems like a lot of work as you will need to set up a web service but it is the BEST way to make sure data is kept safe and secure and can be restored and backed up at any point.
For a web service there are lots of options available to you including cloud services such as Google docs or writing your own website. Ruby on Rails is a great solution for developing your own site as you get a full RESTfull api out of the box and it;'s dead easy to secure/lock down a rails site to authorised users only with a couple of lines of code and with Heroku you can get free hosting.
As usual with Android development the simplest of requirements actually ends up being the most difficult to implement but where data safety is paramount then it's worth the effort to do it properly.
The question is too open to answer simply because the changes that may apply to the db file content are open and one can't guarantee a specific behavior .
On the positive side sqlite project is an open source and the format of the DB file is specified Here
After taking a look there, it seems very possible/not too complicated to parse any DB file looking for Data Only and write it/dump it to another functional db file.
I believe this is the fastest and cleanest solution to the issue in hand.
so to wrap up:
Copy DB file everytime you want to back it up.
When you want to restore create a new DB using Android APIs.
Parse the data from the backed up file and write them to the newly created DB.
P.S:
regarding how to use
PRAGMA journal_mode = DELETE
Simply use db.exec("PRAGMA journal_mode = DELETE"); when creating the DB
I want to make database backup to my app, so I'm looking for the best way.
I should save a copy of the database and programatically make the backup?
Can make database backup with backup manager?
Any tips?
If you are going just to let the user save current state of the application for some reasons, db file(s) copying will be fine. Your can find plenty of samples here, like this: https://stackoverflow.com/a/2251647/812046
But if you need to restore your data on another devices, you should think of anything like sqlite .dump and you'll have to implement it yourself. As I know if you don't have root you will not be able to use native sqlite dump.
Once I used simple csv files to copy data between android device and openbsd machine. Worked fine for small amounts of data.
I have a huge set of data that I want to insert into the sqlite database before the user is able to do anything inside my application. Right now I have all my data stored in CSV files, then I parse those files and use DatabaseUtils.InsertHelper to do a bulk insertion, but this is taking to long to complete.
I stumbled on this tutorial some time ago and I'm wondering: is it safe to distribute a pre-generated sqlite file? Can I run into problems due to different versions of SQLite on different devices?
I'm planning to support Android 2.1 and higher.
I suppose it depends on your definition of safe. It is certainly possible as long as the database conforms to the metadata table spec Android expects, which is what that tutorial you stumbled upon is showing you. You won't have to worry about version conflicts with SQLite as that is a package built into the core platform and isn't something OEMs add to or implement anything on top of.
However, if by safe you mean "protected" you would need to take special steps to ensure that your database is not externally readable if that is a concern. If you simply place the preconstructed DB into assets/ and copy it over, anyone who can properly deconstruct an APK file can view your database data. This may or may not be an issue for you.
The best approach is to populate this data in the database, keep the database in assets & then copy it to the device ... You can follow this complete sample code here.