Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to download an SQLite database from an Android device?

Tags:

android

sqlite

Before spending hours trying to figure this out, maybe I can use someone's expertise; would it be possible to generate an SQLite database on a (web)server and download it to an Android device and then use it?

I need to sync data, but it probably would be much faster to create the database completely and send that as binary.

like image 806
tbeernot Avatar asked Aug 23 '11 13:08

tbeernot


People also ask

Where is SQLite database stored in android mobile?

The Android SDK provides dedicated APIs that allow developers to use SQLite databases in their applications. The SQLite files are generally stored on the internal storage under /data/data/<packageName>/databases.

How save and retrieve data from SQLite database in android?

We can retrieve anything from database using an object of the Cursor class. We will call a method of this class called rawQuery and it will return a resultset with the cursor pointing to the table. We can move the cursor forward and retrieve the data. This method return the total number of columns of the table.


2 Answers

Your accepted solution seemed to take a long time when I tried downloading a file with it. I found a better solution (with progress indicator) here:

http://www.hassanpur.com/blog/2011/04/android-development-downloading-a-file-from-the-web/

It includes source code and step-by-step tutorial.

like image 35
gonzobrains Avatar answered Sep 28 '22 04:09

gonzobrains


Here's my implementation:

     private static boolean downloadDatabase(Context context) {
                try {
                       // Log.d(TAG, "downloading database");
                        URL url = new URL("http://some-url.com/db/" + "db_name.s3db");
                        /* Open a connection to that URL. */
                        URLConnection ucon = url.openConnection();
                        /*
                         * Define InputStreams to read from the URLConnection.
                         */
                        InputStream is = ucon.getInputStream();
                        BufferedInputStream bis = new BufferedInputStream(is);
                        /*
                         * Read bytes to the Buffer until there is nothing more to read(-1).
                         */
                        ByteArrayBuffer baf = new ByteArrayBuffer(50);
                        int current = 0;
                        while ((current = bis.read()) != -1) {
                                baf.append((byte) current);
                        }

                        /* Convert the Bytes read to a String. */
                        FileOutputStream fos = null;
                        // Select storage location
                        fos = context.openFileOutput("db_name.s3db", Context.MODE_PRIVATE); 

                        fos.write(baf.toByteArray());
                        fos.close();
                       // Log.d(TAG, "downloaded");
                } catch (IOException e) {
                        Log.e(TAG, "downloadDatabase Error: " , e);
                        return false;
                }  catch (NullPointerException e) {
                        Log.e(TAG, "downloadDatabase Error: " , e);
                        return false;
                } catch (Exception e){
                        Log.e(TAG, "downloadDatabase Error: " , e);
                        return false;
                }
                return true;
        }

This downloads the database into your applications private storage see here

Don't forget you need the internet permission in your manifest.

To then get an actual database from this file you can do this:

   /**
     * Copies your database from your local downloaded database that is copied from the server 
     * into the just created empty database in the
     * system folder, from where it can be accessed and handled.
     * This is done by transfering bytestream.
     * */
        private void copyServerDatabase() {
            // by calling this line an empty database will be created into the default system path
            // of this app - we will then overwrite this with the database from the server
            SQLiteDatabase db = getReadableDatabase();
            db.close();


                OutputStream os = null;
                InputStream is = null;
                try {
                      // Log.d(TAG, "Copying DB from server version into app");
                        is = mContext.openFileInput("db_name.s3db");
                        os = new FileOutputStream("/data/data/your.package.name/databases/"); // XXX change this

                        copyFile(os, is);
                } catch (Exception e) {
                        Log.e(TAG, "Server Database was not found - did it download correctly?", e);                          
                } finally {
                        try {
                                //Close the streams
                                if(os != null){
                                        os.close();
                                }
                                if(is != null){
                                        is.close();
                                }
                        } catch (IOException e) {
                                Log.e(TAG, "failed to close databases");
                        }
                }
                  // Log.d(TAG, "Done Copying DB from server");
        }




     private void copyFile(OutputStream os, InputStream is) throws IOException {
            byte[] buffer = new byte[1024];
            int length;
            while((length = is.read(buffer))>0){
                    os.write(buffer, 0, length);
            }
            os.flush();
    }
like image 79
Blundell Avatar answered Sep 28 '22 04:09

Blundell