0

I have an existing database that I've already created that I would like to use with my android app. I have put the folder into my assets folder and have the DataBaseHelper.java file as well. Now I would like to retrieve the data and then display that data in one of my activities that I have, for example, in a listView or any other way I can display the data. So does anyone know how I can do that? What are the next steps in order to achieve that?

Ankit Kaushal
  • 11
  • 1
  • 7

3 Answers3

0

Here are some tips to get you going.

  • Run the following SQL on your DB file:
CREATE TABLE "android_metadata" ("locale" TEXT DEFAULT 'en_US');
INSERT INTO "android_metadata" VALUES ('en_US');
  • Place your database (SQLite file, I'm assuming) into assets/
  • I would then recommend copying the file in your application into /data/data/your.package.name/databases/. Here is sample code to do that:
/**
 * Copies your database from your local assets-folder
 * to 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 copyDatabase() throws IOException{

  //Open your local db as the input stream
  InputStream myInput = context.getAssets().open(DB_NAME);

  // Path to the just created empty db
  String outFileName = DB_PATH + DB_NAME;

  //Open the empty db as the output stream
  OutputStream myOutput = new FileOutputStream(outFileName);

  //transfer bytes from the inputfile to the outputfile
  byte[] buffer = new byte[1024];
  int length;
  while ((length = myInput.read(buffer))>0){
      myOutput.write(buffer, 0, length);
  }

  //Close the streams
  myOutput.flush();
  myOutput.close();
  myInput.close();
 }

You can then open the produced file in DataBaseHelper.

Dan Osipov
  • 1,429
  • 12
  • 15
  • So after I run that query that you put there, then I will have that android _metadata in addition to the tables that I already have right? And for that sample code, which java file would I put that into? Would I like put that into the DataBaseHelper.java file? – Ankit Kaushal Apr 07 '15 at 05:02
  • Yes, android_metadata is required by Android to be present in the DB file. And yes, you can put the copy method into the helper, and execute it when the user first installs the app (if there is no database available). – Dan Osipov Apr 07 '15 at 13:36
  • Ok, then once I do that, then how can I get the data from the database using a query and then display that in let's say a listview for example in one of my app's activities, do you know how I can do that? This is where my team members and I are having trouble figuring out. – Ankit Kaushal Apr 07 '15 at 20:50
  • After this point its no different from how you'd normally do it in Android. – Dan Osipov Apr 07 '15 at 22:09
0

The simplest way to do this is using the following project

https://github.com/jgilfelt/android-sqlite-asset-helper

The alternative is to manage the file manipulation yourself, copying the database from the assets into the correct location and ensuring the correct permissions to open the database file.

BrentM
  • 5,671
  • 3
  • 31
  • 38
0

You can try below url for copy DB from assets

http://stackoverflow.com/questions/5945196/database-not-copying-from-assets