• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps android copy of sqlite db?

creatiive

Lurker
hey,

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 ;

"/data/data/PACKAGE_NAME/databases/";

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!!
thanks
------------------

(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:
[LEFT]private void copyDataBase() throws IOException{[/LEFT]
 
[LEFT]//Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);[/LEFT]
 
[LEFT]// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;[/LEFT]
 
[LEFT]//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);[/LEFT]
 
[LEFT]//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);
}[/LEFT]
 
[LEFT]//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();[/LEFT]
 
}


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

Code:
        // Path to the just created empty db
        String outFileName = DB_PATH + DB_NAME;
 
        //if the path doesn't exist first, create it
        File f = new File( DB_PATH );
        if ( !f.exists() )
            f.mkdir();

        //Open the empty db as the output stream
        OutputStream myOutput = new FileOutputStream(outFileName);
 
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.
 
Back
Top Bottom