3

I am new to android app development and i am creating an app which creates a database. I have a few question regarding this.

  1. Each time i launch my app does my app create a new database or will it use the previous database.

  2. If it does then how to stop it and use previous database.

  3. I am using physical device to run my app instead of using an emulator... how do i view where is my database

following is my code

public class ExternalDbOpenHelper extends SQLiteOpenHelper {
    public static final String DB_Name = "mydatabase";
    public static final String Table_Name = "Records";

    public ExternalDbOpenHelper(Context context) {
        super(context, DB_Name, null, 1);
        SQLiteDatabase db = this.getWritableDatabase();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table " + Table_Name + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, col1 text) ");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("Drop table if exists " + Table_Name);
    }
}
C1pher
  • 1,933
  • 6
  • 33
  • 52
Tushar Saha
  • 1,978
  • 24
  • 29

4 Answers4

2

No your database isn't destroyed each time you launch the app. It will however once you update the database version number because of how you have it now with the drop table.

As far as viewing your database, when you connect your phone to your computer, you should be able to see your device in the ADB and the database file will be stored in your package name folder.

You can then open that file in any database viewer (Sequel Pro, etc). If you're using Android Studio I believe you can do all this in Android Device Monitor under Tools -> Android

askilondz
  • 3,264
  • 2
  • 28
  • 40
2

Your database is created with first time you use it. Later same database file is used by Android.

Even after upgrading version of database it is not deleted by itself, what will happen depends on

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    //here you can migrate your data or just drop all tables
}

To see your database on real device without root you can use method provided in another thread: android adb, retrieve database using run-as

If you run your app on Emulator you can see database file directly.

Community
  • 1
  • 1
jakubbialkowski
  • 1,546
  • 16
  • 24
0

@Tushar saha if you wanna experiment a little bit then go ahead with installing your app on emulator with sdk level 23 or below there is some issue in sdk level 25 and above and then install the SqliteBrowser tool for browsing DB. For opening the DB file go to Tools-> Android -> Android Device Monitor

Inside Android Device Monitor select DDMS mode select your device in left pane and then in filemanager select data -> data -> package name -> database -> databasefile.db and then export that file to your computer device and using the above tool import this file now you will be able to see all the tables inside the db. (there will be only 1 db as it is a disadvantage of sqlite as of today that you can't have more than one database).

Then in the first case execute create db query everytime you login while the other time only execute it once and then see if the database is there or not.

NOTE: you will not be able to see data inside filemanager if you install app on your physical device you need an emulator with sdk level 23 or below as of today.

cammando
  • 596
  • 7
  • 20
0

Some of the answers blur the meaning of database with the tables in the database.

No your database isn't destroyed each time you launch the app. It will however once you update the database version number because of how you have it now with the drop table.

I don't believe the database is destroyed in the "onUpgrade" method of the OP, just a table in the database is destroyed and recreated - a very different concept, since the version number should not be reset to 0.

I would like to understand this better also, particularly when using an Android Developer 3.6 emulator. No matter what version number I pass to the SQLiteOpenHelper super() statement in the constructor, it seems to recreate a database file and call onCreate() instead of onUpgrade(). I would like the emulator to persist the database file somehow so the behavior would call onUpgrade() if I run the app in the emulator with a new database version. Perhaps it has to do with running the emulator, whether it does a complete reinstall of the app including the database file, or just updates the app software. I'm new to Android so learning.

sb4
  • 169
  • 1
  • 6