开发者

Database seems to disappear after my application downloads it. Ideas?

开发者 https://www.devze.com 2023-01-12 00:34 出处:网络
One of my applications downloads a database from a server. When I install the application onto my phone, it downloads the file correctly and loads the information, no exceptions thrown or anything.

One of my applications downloads a database from a server. When I install the application onto my phone, it downloads the file correctly and loads the information, no exceptions thrown or anything.

However, when I upload the apk into the Android Market Place and download it onto the phone, the application downloads the database and then crashes, saying that the sqlite handler was not ab开发者_如何转开发le to open up the database.

Here's the progression of code:

In the SQLiteOpenHelper:

this.FULL_DB_PATH = new String(this.getWritableDatabase().getPath());
this.getWritableDatabase();

// Code for retrieving the database off of the server:

private void copyDataBase(){
        InputStream myInput=null;
        OutputStream myOutput=null;
        try{
            // opening connections
            URL url = new URL("server_url_here");
            URLConnection ucon=url.openConnection();
            myInput = ucon.getInputStream();     
            myOutput = new FileOutputStream(this.FULL_DB_PATH);

            // write to the db here
            byte[] buffer = new byte[1024*1024];
            int length;
            while ((length = myInput.read(buffer))>0){
                myOutput.write(buffer, 0, length);
            }

            myOutput.flush();
            myOutput.close();
            myInput.close();
        }
         catch(Exception e)
         {

                try{
                    myOutput.flush();
                    myOutput.close();
                    myInput.close();
                }
                catch(Exception es){}
         }
    }

I don't know why my colleague is saving the db with a mp3 extension but...it works when we're installing the apk ad-hoc.

For:

myOutput = new FileOutputStream(this.FULL_DB_PATH);

I've also tried:

myOutput = this.getApplicationContext().openFileOutput(this.FULL_DB_PATH, Context.MODE_WORLD_READABLE);

But that doesn't work either.

Any help is greatly appreciated!! I've been tearing my hair out for a couple of hours over this and I just want it to end haha.


What you are doing is copying the file into the /data folder in the android file system. On a normal phone with normal permissions this is not allowed for security reasons. You can do this on the emulator and rooted phones because you have been granted permission to write to /data (normally only read).

To get around this you will need to manually add the information to the database using db.insert(String, String, ContentValues) and db.update(String, ContentValues, String, String[])

It would be nice to be able to do what you are trying but for security reasons it's a very good thing you can't.


The Emulator gives you special access that real phones don't. In other words, I suspect you are directly copying the file into the databases directory on the phone? If so, you can't do that on a non-rooted phone. Hopefully someone will chime in and prove me wrong because this is a problem for me too. I'd like to easily download a db file and install it. What I end up having to do as a work-around is create a new database and load the schema and data through queries.

EDIT: I would like to share a trick with you. It's simple really. Instead of using just db.insert/db.update/ by themselves, scope them into transactions. In fact, I would scope the whole DB Build as a transaction, or at least just individual tables. I've experienced a 10-50x increase in speed of transactions when they're scoped and then committed all at once.


The other two answers are incorrect, I do believe. I have a backup system in my app where backups are copied to the sd-card, and then copied back to the /data/-folder when the backup is restored. While I have no idea why your code wont work, here is my code to copy the database from the sd-card:

final InputStream in = new FileInputStream(from);
final OutputStream out = new FileOutputStream(to);
final byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0){
    out.write(buf, 0, len);
}
in.close();
out.close();

Just to closer investigare the error, try to download to the sd-card instead and then use my code to copy it to the data-folder. The "to" object in my code I get like this:

public static final String DATABASE_PATH = "/data/data/"+SomeClass.class.getPackage().getName()+"/databases/"+ DATABASE_NAME;
final File to = new File(Constant.DATABASE_PATH);
0

精彩评论

暂无评论...
验证码 换一张
取 消