7

I have an android application that is supposed to read and expand a database that is already created on sqlite...it works fine on emulator by putting database in "data/data/(packagename)/database" folder on the file explorer of emulator. Now problem is occuring with the real device. Obviously it doesnt have the database to open.I tried to put database in assets folder but I am not getting to open it with the openhelper.

Anas Shahid
  • 353
  • 2
  • 5
  • 10

5 Answers5

6

you should copy the .db file from your assets folder to an internal/external storage. You can use 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"); 
     } 
  } 
} 

Check that 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 the 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(); 
}

i hope it should help you.

Gva
  • 114
  • 3
  • 1
    You never mention why it does not work straight from the assets folder. Could you expand on this? – Radu Apr 21 '14 at 16:06
  • This is a really good question I too would like to know why is copying needed when you get access to files inside assets folder? – Igor Čordaš May 05 '14 at 13:26
  • 1
    The assets folder is ready only after compile time, and we cannot modify the data in it. If you want to write to the database form our application, we must first copy it to an internal/external storage. – Gva May 13 '14 at 11:58
  • I believe the folder in your DB_PATH String should have a plural "databases" – not "database" – rrbrambley May 04 '16 at 17:23
3

you cant access the database from asset folder directly you need to copy it first to the path data/data/(packagename)/database then using it :

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

in your onCreate()

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

Then the method to call:

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();
    }
}
mmoghrabi
  • 1,233
  • 1
  • 14
  • 23
1

You need to first copy the Database file from assests to application data location using java code.Can You Post some code to show How are you opening or handling the database?

Ansh
  • 2,366
  • 3
  • 31
  • 51
0

You cannot directly open files from assets folder. Instead, you need to copy the contents of your assets folder on an internal/external storage and later use the File path to open the file. In emulators, its easier for you to access the data folder of your apps. However, on a real non-rooted android device, its not possible due to security reasons.

waqaslam
  • 67,549
  • 16
  • 165
  • 178
  • yes I tried that too..but my code doesnt seem to be working.may be its because i want to access multiple tables – Anas Shahid Jun 18 '13 at 03:59
  • you can access as many tables as you want without provoking deadlocks in the database. it depends how you tried. if you post some code, that would be easier to debug – waqaslam Jun 18 '13 at 06:52
0

Do you have a pre-populated database and looking to integrate into your app? If yes, you can simply do with my library

On your app's 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);

Simply fire SQL queries

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

Get results on Array 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
}

You need to include my library for this. Documentations here:
https://github.com/sangeethnandakumar/TestTube

elixenide
  • 44,308
  • 16
  • 74
  • 100
Almero Rick
  • 118
  • 1
  • 11