34

I am trying to use Database Inspector in Android Studio. Why I run the app on the device, it inspector is always showing my application database (highlighted in the image) as "closed".

Is there any fix for this or did I miss something during the setup?

enter image description here

1

15 Answers 15

18

UPD. last time I had this annoying issue Invalidate cache/Restart fixed this for me. Sigh

Running the app in Debug mode enter image description here (Win: Shift+F9 or Mac: Control+D) does the magic for me sometimes.

Not sure if this is a bug or not. The android documentation doesn't mention on this, so I guess it should just work in normal mode, but for some reason doesn't for me..

I use (Android Studio 4.2 beta 3). Property minSdkVersion set to 27.

I don't use Room or any other ORM, just a few plain simplest queries.

The toggle 'Keep database connection open' doesn't seem helping either.

In normal mode:

enter image description here

In Debug mode (what I want):

enter image description here

Noticeable, that it continuing to work (querying the database etc) even after I stopped the app running in Debug mode:

enter image description here

3
  • I tried the above mentioned steps but didn't work. I clear the app cache and data and restarted the app and it started working.
    – Zahid Ali
    Nov 29, 2021 at 7:21
  • 1
    interesting, in my case the problem was that the database was too large (15+ gb), once i cleared it - this inspection feature started working as expected
    – konnovdev
    May 23, 2022 at 4:54
  • Invalidating cache + restart + app uninstall = didn't work. But probably helped doing simple select on DB before connecting 'App inspection'. Not sure ;] Dec 8, 2022 at 19:43
16

I was having same issue, i tried lot's of thing but didn't work and finally a day after i come to know the issue. Issue was that i was using encrypted SQLite database (Custom openHelperFactory).

For me it was the issue after removing openHelperFactory

It works perfectly fine, My answer might not be not useful for you but it would be useful for someone for sure.

Thank you

2
6
  1. Click on your close database icon
  2. Enable (keep open) => enter image description here
  3. Do some action related to the database(for example get a list of ...)
  4. The database stays in open mode
5

For me the reason for "closed" was - most probably - that the framework I use (Exposed/SQLDroid) closes the database connection when it is not working.

So, even when everything worked fine in the application, the database was closed at the time the Database Inspector wanted to look into it.

Adding the line below basically solved the problem. I wouldn't put this into production code as I haven't checked what happens in details, but it helped during development.

val db = openOrCreateDatabase("testdb.db", MODE_PRIVATE, null)
// db.close() -- DO NOT USE THIS

Important thing is not to use db.close(). When db.close() is there, Database Inspector shows the db as closed. When it is not there, Database Inspector usually works.

2

In my case, I'm using SQLite database. I've just comment this line :

//yourDatabaseName.close();

Then run again and hope it works.

Don't forget to uncomment the line after you finish.

2

Enable "keep database connection open". See the image below

Database View

0
2

None of the posted solutions worked for me until I ran the app with a debugger instance attached then terminated the app right after the app loads on the emulator.

After that, navigating to the Database Inspector view shows the necessary data: enter image description here

1
  • I was going to answer this when I saw it last in the list. Got my up vote.
    – John T
    May 31, 2022 at 8:19
1

Not sure if this will help, but there is a chance you have selected the wrong emulator/device for database inspector. enter image description here

You can change the emulator for your database inspector by clicking on the name of the emulator as shown in the image.

1

In case you are using Drift flutter database, changing the openConnection function to the code below fixed the issue for me:

LazyDatabase _openConnection() {
  return LazyDatabase(() async {
    final dbFolder = await getDatabasesPath();
    final file = File(p.join(dbFolder, 'db.sqlite'));

    if(Platform.isAndroid){
      return SqfliteQueryExecutor.inDatabaseFolder(path: 'db.sqlite');
    }else {
      return NativeDatabase(file);
    }
  });
}

Note that you will need to import sqflite in your pubspec.yaml

I found the solution in an Issue from the official github repo: https://github.com/simolus3/drift/discussions/1818

1

Sometimes a database is autoclosed and needs to force-open the database.

if (instance == null) {
        instance = Room.databaseBuilder(
                context,
                TheDatabase.class,
                DATABASE_NAME
        )
                .addCallback(callback)
                .allowMainThreadQueries()
                .build();
    }
    instance.getOpenHelper().getWritableDatabase(); //<<<<< FORCE OPEN
    return instance;
1

Weird but this is what worked for me:

  1. I killed the app (what was running under Android Studio's debugger).
  2. That immediately prompted a security warning dialog box which asked whether I confirm downloading the database from the smartphone to Android Studio. I clicked 'Yes'.
  3. Restarting the app (via debugger) found the database closed again.
  4. But killing the app again, magically displayed the app's .db as open, along with its tables.

I believe this is what's called "Offline mode" in https://developer.android.com/studio/inspect/database#offline-mode

I don't have an explanation for this yet but this is what worked for me on Android Studio 2021.3 (against an Android 13 target).

0

I don't have a great answer, but here are some other possible approaches:

From your screenshot, I see you've done this already, but for others... it sounds like this is how you're supposed to be able to make this work -

Additionally, you can prevent database connections from closing by toggling Keep database connections open from off () to on () at the top of the Databases pane.

... but that didn't work for me, so I end up going to the Device File Explorer, saving the database I'm interested in to my desktop, and working with it there.

I've also had good luck with this tool: https://github.com/amitshekhariitbhu/Android-Debug-Database

0

Updating the Room version (currently 2.2.6) did the trick for me, but there's no mention to any such a bug in the release notes. I suspect it may have something to do with caches so maybe that's also worth trying

0

Actually Debug mode does not work for me.

It worked initially. And suddenly after some successful build, again that issue came. So what I do is stop inspection and open the page where you actually query the db and then attach from app inspection, it shows DB open all the time.

0

For me answer was that when you initialize(!!!!) your DatabaseHelper (or whatever it's called in your programm) Multiple times it shows database is closed, even if u dont use any of initialized variables. It may be obvious but i'm new to Android(i'm still a student) and u just might have let it unnoticed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.