What is the best way to backup app database to a file?

by Moto » Mon, 07 Sep 2009 03:23:47 GMT


Sponsored Links
 I need to somehow backup my applitcations database to a file so that
it can later be retored using that file.  Any suggestions to doing
this?

The only way I would do it would be by reading every table to an xml
ffile but it seems soo ineficient...  The data does not have to be
humand readable...

Thanks!
-Moto!
--~--~---------~--~----~------------~-------~--~----~



What is the best way to backup app database to a file?

by Mark Murphy » Mon, 07 Sep 2009 03:31:35 GMT


 


One approach is to copy the database file.

Tactically, new File(getFilesDir(), "../databases") is the directory
where your database file resides. Copy it using ordinary Java I/O,
though I recommend you not hold any open SQLiteDatabase objects on the
database at the time.

Now, the database path location may change in future Android releases,
and I am not aware of a place in the API to get the path to a database
in a more official fashion. Hence, you will need to double-check with
each Android release to confirm they have not moved the databases
someplace else.

-- 
Mark Murphy (a Commons Guy)
 http://commonsware.com  |  http://twitter.com/commonsguy 

Looking for Android opportunities?  http://wiki.andmob.org/hado 

--~--~---------~--~----~------------~-------~--~----~


Sponsored Links


What is the best way to backup app database to a file?

by Moto » Mon, 07 Sep 2009 03:41:07 GMT


 So, I can actually access the database as a file copy and than restore
it by moving it to the same location where it was copied from?

Thanks!





--~--~---------~--~----~------------~-------~--~----~



What is the best way to backup app database to a file?

by Mark Murphy » Mon, 07 Sep 2009 03:42:55 GMT


 


AFAIK, it should work fine, so long as it is your app doing the copying,
and you are not trying to use the database while copying (in either
direction).

-- 
Mark Murphy (a Commons Guy)
 http://commonsware.com  |  http://twitter.com/commonsguy 

Looking for Android opportunities?  http://wiki.andmob.org/hado 

--~--~---------~--~----~------------~-------~--~----~



What is the best way to backup app database to a file?

by Moto » Mon, 07 Sep 2009 03:48:51 GMT


 Great this will be perfect!
Thanks Mark!
--~--~---------~--~----~------------~-------~--~----~



What is the best way to backup app database to a file?

by Roman ( T-Mobile USA) » Mon, 07 Sep 2009 04:18:20 GMT


 Are you right now thinking only to backup your data base on the device
or also somewhere on a remote location (PC, server)?

You could easily do a byte copy of your DB

        //open your existing DB
        InputStream dbStream = mContext.getAssets().open(<yourDBName>);

        OutputStream backupDbStream = new FileOutputStream(<path+name of
your backup DB>);

        //copy file from your existing DB to your backup DB
        byte[] buffer = new byte[1024];
        int length;
        while ((length = dbStream.read(buffer))>0){
                backupDbStream.write(buffer, 0, length);
        }


If you want to support backing up the DB on a remote system, you could
easily use the same mechanism.

--
Roman Baumgaertner
Sr. SW Engineer-OSDC
T  Mobile stick together
The views, opinions and statements in this email are those of the
author solely in their individual capacity, and do not necessarily
represent those of T-Mobile USA, Inc.






--~--~---------~--~----~------------~-------~--~----~



What is the best way to backup app database to a file?

by mjc147 » Tue, 08 Sep 2009 13:06:18 GMT


 


You can use the Context.getDatabasePath() method:

 http://developer.android.com/reference/android/content/Context.html #getDatabasePath(java.lang.String)


--~--~---------~--~----~------------~-------~--~----~



What is the best way to backup app database to a file?

by Moto » Wed, 09 Sep 2009 00:38:07 GMT


 Perfect!
I really appreciate everyone's help!

-Moto!





--~--~---------~--~----~------------~-------~--~----~



What is the best way to backup app database to a file?

by Moto » Mon, 21 Sep 2009 02:21:15 GMT


 When doing:

//open your existing DB
InputStream dbStream = mContext.getAssets().open(<yourDBName>);

I'm not able to start reading...  Do I need a particular permission
type? Also, when writing back to that location a new database which
permission should be set?

Thanks,
-Moto!





--~--~---------~--~----~------------~-------~--~----~



What is the best way to backup app database to a file?

by Moto » Mon, 21 Sep 2009 02:30:37 GMT


 I take that back...

It actually works without any permission as it should...

File f1 = ctx.getDatabasePath("appDatabase.db");
InputStream in = new FileInputStream(f1);

-Jona







--~--~---------~--~----~------------~-------~--~----~



What is the best way to backup app database to a file?

by Moto » Mon, 21 Sep 2009 02:31:15 GMT


 I take that back...

It actually works without any permission as it should...

File f1 = ctx.getDatabasePath("appDatabase.db");
InputStream in = new FileInputStream(f1);

- Moto!






--~--~---------~--~----~------------~-------~--~----~



Other Threads

1. How many threads can be created or how to avoid thread leak

HI,

I am developing a chat application. Everytime a user clicks a button a
thread needs to handle it as text message needs to be sent over
network. so, I am creating a new thread each time when the user clicks
on button.  I wish only 1 thread handles all the messages to be sent.
But, resume/suspend/stop are all deprecated.

is there a way to do it.

currently, I am doing it like the following.

<code>
View.OnClickListener showChat = new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                                t_send = new Thread() {
                                        public void run() {
                                                recv = false;
                                                m_mychat = doExpensiveSend();

                                                mHandler2.post(mUpdateResults2);
                                        }
                                };
                                t_send.start();

                        }
                };
</code>
--~--~---------~--~----~------------~-------~--~----~

2. The Economic Crisis and Android

It means Free Android Phones supported by Advertising! ..Whenever the
crisis ends, then everyone in the world will end up with an Android
Phone.


- Juan T.
--~--~---------~--~----~------------~-------~--~----~

3. What Phones does Android Support?

4. JNI support in 1.0

5. skia : error in decode file

6. Question about sharing PIM data

7. T-Mobile Flash based Android Emulator