1

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!

tshepang
  • 12,111
  • 21
  • 91
  • 136
Fabiow
  • 11
  • 3

2 Answers2

0

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Hi, thanks for the reply. I actually uninstalled it through settings > ... > (my app) > uninstall. I tried both ways, clearing data and uninstalling app, but got the result shown above. And I didn't change the database location, it is in the data/data/... directory. And about the logcat, shouldn't it automatically throw an exception if the sql command was wrong? Because after creating the tables, i'm inserting rows with a column that is not in the previous (wrong) category table: image. Do you have any further suggestions? :/ – Fabiow Dec 07 '13 at 15:19
  • @user3077684: "And about the logcat, shouldn't it automatically throw an exception if the sql command was wrong?" -- yes, and so your SQL is not wrong. After an uninstall, use DDMS File Explorer to confirm that you no longer have the `/data/data/` directory associated with your app. If the directory is still there, you did not uninstall the app, or your emulator is seriously messed up. If the directory is gone, then your code for `onCreate()` is executing your old SQL, not the new SQL, when it is creating the new database. – CommonsWare Dec 07 '13 at 15:29
  • to do quick uninstall, just write in the command line (again assuming environment variable) adb uninstall com.mypackage.myapp this is way faster than going to settings and deleting app from there – Lena Bru Dec 07 '13 at 16:15
  • It doesn't seem to make sense: Each time I (un/re)install it, a different thing happens: in the main activity, I checked that the database was actually deleted. When I go to an activity that requires database information, right before the execSQL() method, I can see that: either a brand new database was created or a new database with the old sql command is created... CommonsWare, I what you mean with "then your code for onCreate() is executing your old SQL" Seems like it's exactly what's happening... How can the old sql be executed? is it cached somewhere? When does it happen? – Fabiow Dec 07 '13 at 20:25
  • @Fabiow: "How can the old sql be executed?" -- bug in the app, presumably. – CommonsWare Dec 07 '13 at 20:32
  • But the only time execSQL is exectudet is onCreate() and OnUpgarde() :/ oh well – Fabiow Dec 08 '13 at 12:33
0

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!

Lena Bru
  • 13,521
  • 11
  • 61
  • 126
  • Hi Lena, what you're doing is copying the databse to somewehre else, right? I' tried [this](http://stackoverflow.com/questions/4867379/android-eclipse-ddms-cant-access-data-data-on-phone-to-pull-files) post, but it gives "permission denied" on adb shell -> su... And I'm using Ubuntu, I don't know if the procedure is the same, I'll try to figure it out. Thanks! – Fabiow Dec 07 '13 at 15:34
  • no su is needed , you are , from within your app, copying the database to your sdcard, and then from the command line you are downloading the file and doing whatever you like with it, i gave you this solution because you wrote "i dont have access to db on device" – Lena Bru Dec 07 '13 at 15:42