Handling Android Database on a Virtual Device

152 views Asked by At

I've been having a LOT of trouble with Android database since I started my app two months ago... I went through many tutorials, but only few questions were answered... I gave up using my (real) device database, 'cause I couldn't have access to it, so I started using an emulator. But something weird happens. I have this class:

public class DatabaseHandler extends SQLiteOpenHelper
{
    @Override
    public void onCreate(SQLiteDatabase database)
    {
        database.execSQL(sql_command);
    }
}

Whenever I uninstall the app, the database should be completely deleted from the (virtual) device, correct? But when I reinstall it and put a breakpoint in the execSQL() line (before the method is executed), I can see through SQLite Manager tab that the database is as it was before: The category table has only the _id, name, description, current_level and status.

The current sql command is (Notice the new image column int the Category table):

create table categories(
_id integer primary key autoincrement,
name text not null,
description text not null,
current_level integer DEFAULT 0,
status integer DEFAULT 0,
image text not null);

create table levels(
_id integer primary key autoincrement, 
category_id integer not null, 
level integer, 
total_questions integer, 
answered_questions integer DEFAULT 0, 
correct_answers integer DEFAULT 0, 
time integer DEFAULT 0);

Not only the image column is inserted, but also the level table doesn't show on the database. Besides, when I run the execSQL() method, nothing shows on the logcat tab.

By the way, I'm using Ubuntu 12.10, Eclipse Juno and Android 2.2!

2

There are 2 answers

6
CommonsWare On

Whenever I uninstall the app, the database should be completely deleted from the (virtual) device, correct?

If you actually are uninstalling it, and if the database is in its normal location on internal storage, then yes.

Note that simply running your modified app does not uninstall the old version. It replaces the old version, but all data is kept intact.

To get rid of your existing database on internal storage, you can:

  • Truly uninstall the app, or

  • Click on Clear Data for your app in Settings

If you have altered the behavior of SQLiteOpenHelper to put the database on external storage, then the database will only be removed on uninstall if it is in getExternalFilesDir(), getExternalCacheDir(), or some subdirectory of those. If you put the database in, say, the root of external storage, then the database will remain intact after an uninstall.

Besides, when I run the execSQL() method, nothing shows on the logcat tab.

In your code shown above, you are not logging anything to LogCat.

2
Lena Bru On

I don't know about the virtual device - but this is what i use in order to get access to my database from my device

i put this in one of the activities after the database has been created

if (COPY_DB) {
            try {
                File sd = Environment.getExternalStorageDirectory();
                String databaseName ="myDbName";

                if (sd.canWrite()) {
                    String currentDBPath = Environment.getDataDirectory() + "/data/" + getPackageName() + "/databases/"+databaseName;
                    String backupDBPath = 

                   Environment.getExternalStorageDirectory().getPath() +"/"+ databaseName+".db";
                    Log.d(TAG,"backupPath : "+backupDBPath);
                    File currentDB = new File(currentDBPath);
                    File backupDB = new File(sd, backupDBPath);
                    if (!backupDB.getParentFile().exists()) {
                        backupDB.getParentFile().mkdirs();
                    }

                    if (currentDB.exists()) {
                        FileInputStream fisc = new FileInputStream(currentDB);
                        FileOutputStream fosc = new FileOutputStream(backupDB);
                        FileChannel src = fisc.getChannel();
                        FileChannel dst = fosc.getChannel();
                        dst.transferFrom(src, 0, src.size());
                        fisc.close();
                        fosc.close();
                        src.close();
                        dst.close();
                    }
                }
            } catch (Exception e) {
                Log.e(TAG, "error", e);
            }

the i open the command line and go to the folder where i want to download my database to

and write

c:\Dev\pulledFiles>adb pull the_path_of_the_file_from_the_log

this assumes your adb has an environment variable, if it doesnt, instead of adb, you need to specify the entire path of it, which is usually /platform-tools/adb

after that i open the database in a database reader like SQLiteBroswer (though the broswer is very buggy) good luck!