0

I am trying to transfer a SQLite database into an app by downloading it and then unzipping it to the correct location. I was successful in transferring the DB when it was unzipped. The error I get is that it cannot find any of the tables I query. I have also been successful in unzipping and reading normal text files.

The DB has Hebrew and English, but that has not caused problems before. The bilingual DB was copied successfully when it was not zipped and bilingual texts have been successfully unzipped and read. Still, it is a possibility that there is an encoding problem going on. That seems weird to me, because as you can see below in the code, I'm just copying the bytes directly.

-EDIT-

Let's say the prezipped db is called test1.db. I zipped it, put it in the app, unzipped it and called that test2.db. when I ran a diff command on these two, there were no differences. So there must be a technical issue with the way android is reading the file / or maybe encoding issue on android that doesn't exist on pc?

I hate to do a code dump, but i will post both my copyDatabase() function (which works). That is what I used previously running it on an unzipped DB file. I put it here as comparison. Now I'm trying to use unzipDatabase() function (which doesn't work), and use it on a zipped DB file. The latter function was copied from How to unzip files programmatically in Android?

private void copyDatabase() throws IOException{
    String DB_NAME = "test.db";
    String DB_PATH = "/data/data/org.myapp.myappname/databases/";
    //Open your local db as the input stream
    InputStream myInput = myContext.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();
}

private boolean unzipDatabase(String path)
{     
    String DB_NAME = "test.zip";
    InputStream is;
    ZipInputStream zis;
    try 
    {
        String filename;
        is = myContext.getAssets().open(DB_NAME);
        zis = new ZipInputStream(is);          
        ZipEntry ze;
        byte[] buffer = new byte[1024];
        int count;

        while ((ze = zis.getNextEntry()) != null) 
        {
            // write to a file
            filename = ze.getName();

            // Need to create directories if not exists, or
            // it will generate an Exception...
            if (ze.isDirectory()) {
                Log.d("yo",path + filename);
                File fmd = new File(path + filename);
                fmd.mkdirs();
                continue;
            }

            OutputStream fout = new FileOutputStream(path + filename);

            // reading and writing zip
            while ((count = zis.read(buffer)) != -1) 
            {
                fout.write(buffer, 0, count);             
            }
            
            fout.flush();
            fout.close();               
            zis.closeEntry();
        }

        zis.close();
    } 
    catch(IOException e)
    {
        e.printStackTrace();
        return false;
    }

    return true;
}
Community
  • 1
  • 1
Noah Santacruz
  • 460
  • 1
  • 3
  • 18
  • yeah, again I was able to copy and read the DB when it was unzipped (in windows). But when I unzip it on the android phone, it cannot find the tables, which indicates to me that the entire file was probably not copied correctly – Noah Santacruz Jan 07 '15 at 10:05
  • The `copyDatabase()` method is not necessary (a time waster), since you can use the `unzipDatabase()` method to extract the db directly into the correct path (just add a String parameter with the destination path). – Phantômaxx Jan 07 '15 at 10:14
  • also do not hardcode DB_PATH ... use some library like assetsdbhelper(something like that) ... and if db is read-only on device you can consider to use some kind of synchronization (one way from server is pretty easy to implement, two way is pain in the a**) with HTTP+JOSN as medium – Selvin Jan 07 '15 at 10:44
  • not sure about what either of those suggestions were, but I'm happy to take them (even though they're irrelevant to this question). Could you post some links in the comments to what you're referring to. The db is read-only. updates are done by downloading a completely new db and overwriting – Noah Santacruz Jan 07 '15 at 11:04

1 Answers1

0

So still don't know why, but the problem is solved if I first delete the old copy of the database (located at DB_PATH + DB_NAME) and then unzip the new one there. I didn't need to do this when copying it directly.

so yay, it was a file overwriting issue...If someone knows why, feel free to comment

Noah Santacruz
  • 460
  • 1
  • 3
  • 18