android copy of sqlite db?

Last Updated:

  1. creatiive

    creatiive Member


    I am using the following code to obtain info from my db;

    int name1Idx = c.getColumnIndex("Name1");
    int name2Idx = c.getColumnIndex("Name2");

    Now, i am packaging a database in my assets folder which have these columns in, however it did not used to. When my program starts , for now i am overwiting my assets folder database to the one that gets autocreated in ;


    essentially, the new column is not getting added, so the database is not getting copied over. It sounds like a simple error but i cant for the life of me track it down. So i was wondering if the emulator cache's a copy of the DB or something? C.getColumnIndex keeps returning -1 for one of my columns that is 100% in my db in my assets folder.

    any help would be awesome!!

    (just for extra info, i copy the database when my program starts like this; I do i like this because its the only way i know to package your app with pre-existing data in it!)

    Code (Text):
    2. [LEFT]private void copyDataBase() throws IOException{[/LEFT]
    4. [LEFT]//Open your local db as the input stream
    5. InputStream myInput = myContext.getAssets().open(DB_NAME);[/LEFT]
    7. [LEFT]// Path to the just created empty db
    8. String outFileName = DB_PATH + DB_NAME;[/LEFT]
    10. [LEFT]//Open the empty db as the output stream
    11. OutputStream myOutput = new FileOutputStream(outFileName);[/LEFT]
    13. [LEFT]//transfer bytes from the inputfile to the outputfile
    14. byte[] buffer = new byte[1024];
    15. int length;
    16. while ((length =>0){
    17. myOutput.write(buffer, 0, length);
    18. }[/LEFT]
    20. [LEFT]//Close the streams
    21. myOutput.flush();
    22. myOutput.close();
    23. myInput.close();[/LEFT]
    25. }


  2. sn00zer

    sn00zer Member

    Why don't you just populate the database when it is created?
  3. sn00zer

    sn00zer Member

  4. freezingblue

    freezingblue New Member

    Make sure you are creating the directory first or your code will fail.

    Code (Text):
    2.         // Path to the just created empty db
    3.         String outFileName = DB_PATH + DB_NAME;
    5.         //if the path doesn't exist first, create it
    6.         File f = new File( DB_PATH );
    7.         if ( !f.exists() )
    8.             f.mkdir();
    10.         //Open the empty db as the output stream
    11.         OutputStream myOutput = new FileOutputStream(outFileName);
  5. oba

    oba New Member

    Hi...newbie to android. I've been reading about packaging the database into the assets folder. I've seen other posts about having to copy the database into a /data/data/pkg/databases folder when the program runs the first time. What is the purpose of this and does this mean the packaged db is duplicated on the phone. Are there any links to show the general methodology of things like this and not simply the coding mechanics of it. Any good links for android just in general. Thanks.

Share This Page