Android external database in the resource folder

I have an Android application that needs to read and expand a database that has already been created in sqlite ... it works fine on the emulator, putting the database in the "data / data / (packagename) / database" folder in the emulator explorer file. Now the problem is with the real device. Obviously, it does not have a database for open.I tried to put the database in the resource folder, but I do not open it with openhelper.

+7
source share
5 answers

you should copy the .db file from the folder with your resources to internal / external storage. You can use the following codes,

private static String DB_PATH = "/data/data/your package/database/"; private static String DB_NAME ="final.db";// Database name 

To create a database,

 public void createDataBase() throws IOException { //If database not exists copy it from the assets boolean mDataBaseExist = checkDataBase(); if(!mDataBaseExist) { try { //Copy the database from assests copyDataBase(); Log.e(TAG, "createDatabase database created"); } catch (IOException mIOException) { throw new Error("ErrorCopyingDataBase"); } } } 

Make sure the database exists here: / data / data / your package / database / DB Name

 private boolean checkDataBase() { File dbFile = new File(DB_PATH + DB_NAME); return dbFile.exists(); } 

Copy database from assets

  private void copyDataBase() throws IOException { InputStream mInput = getApplicationContext().getAssets().open(DB_NAME); String outFileName = DB_PATH + DB_NAME; OutputStream mOutput = new FileOutputStream(outFileName); byte[] mBuffer = new byte[1024]; int mLength; while ((mLength = mInput.read(mBuffer))>0) { mOutput.write(mBuffer, 0, mLength); } mOutput.flush(); mOutput.close(); mInput.close(); } 

Hope this helps you.

+6
source

you cannot access the database from the resource folder directly, you first need to copy it to the / data / (packagename) / path database file using it:

  private String DB_PATH = "/data/data/" + "yourpackaename" + "/databases/" + "db.db"; 

in your onCreate ()

  is = getAssets().open("db.db"); write(is); 

Then the call method:

 public void write(InputStream is) { try { OutputStream out = new FileOutputStream(new File(DB_PATH)); int read = 0; byte[] bytes = new byte[1024]; while ((read = is.read(bytes)) != -1) { out.write(bytes, 0, read); } is.close(); out.flush(); out.close(); System.err.println(out + "\n"); } catch (IOException e) { e.printStackTrace(); } } 
+3
source

You cannot directly open files from the assets folder. Instead, you need to copy the contents of the assets folder to internal / external memory, and then use the File path to open the file. In emulators, itโ€™s easier for you to access the data folder of your applications. However, on a real non-root Android device, it is not possible for security reasons.

0
source

You need to first copy the database file from the competitions to the location of the application data using java code. Can you publish some code to display. How do you open or process a database?

0
source

Do you have a pre-populated database and want to integrate into your application? If yes, you can just do with my library

In your application, the first launch after installation

 SuperDatabase database=new SuperDatabase(getApplicationContext(),"foods.db", AssetDatabaseMode.COPY_TO_SYSTEM); 

On subsequent launches

 SuperDatabase database=new SuperDatabase(getApplicationContext(),"foods.db", AssetDatabaseMode.READ_FROM_DEVICE); 

Just run SQL queries

 database.sqlInject("INSERT INTO food VALUES('Banana','Vitamin A');"); 

Get array results in CSV, JSON, XML

 ArrayList<String> rows=new ArrayList<String>(); rows=database.sqlEjectCSV("SELECT * FROM food;"); for (int i=0;i<rows.size();i++) { //Do stuffs with each row } 

To do this, you need to enable my library. The documentation is here:
https://github.com/sangeethnandakumar/TestTube

0
source

All Articles