3

I am planning on writing a simple Android application that is like a small directory of professors. It will have their name, email, phone and a picture of them. I need to manually send an sqlite file from the server to the phone. I have been trying to research how to do this, but it looks like there are so many ways! I'm hoping someone can point me in the best direction!

jahWalker
  • 31
  • 1
  • 2

1 Answers1

3

The easiest way I can think of, is to open an URLConnection to your server, read the response and save it to your app's database directory (or SD card).

For example:

URL url = new URL("http://example.com/file.sqlite");
URLConnection conn = url.openConnection();
BufferedInputStream bin = new BufferedInputStream(conn.getInputStream());
FileOutputStream fos = 
    new FileOutputStream("/data/data/[your.pkg.name]/databases/file.sqlite");
byte[] buffer = new byte[1024];
int read = 0;
do {
    read = bin.read(buffer, 0, buffer.length);
    if (read > 0)
        fos.write(buffer, 0, read);
} while (read >= 0);

After that you can open your database with a subclass of SQLiteOpenHelper or directly by calling SQLiteDatabase.openDatabase(..):

e.g.:

SQLiteDatabase.openDatabase("/data/data/[your.pkg.name]/databases/file.sqlite",
        null, SQLiteDatabase.OPEN_READWRITE);
rocky3000
  • 1,134
  • 8
  • 9