Android Studio Database Inspector always showing database as "closed"
Asked Answered
M

23

53

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

Medicinal answered 29/11, 2020 at 8:37 Comment(1)
github.com/amitshekhariitbhu/Android-Debug-Database is a good alternative for database inspectorSciatica
F
25

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

Foxing answered 24/1, 2021 at 10:6 Comment(4)
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.Peace
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 expectedFoxhole
Invalidating cache + restart + app uninstall = didn't work. But probably helped doing simple select on DB before connecting 'App inspection'. Not sure ;]Monophony
Invalidate cache/Restart worked for meWiper
M
19

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

Metcalf answered 8/1, 2021 at 13:54 Comment(2)
Related issue issuetracker.google.com/issues/153684328Wallraff
is there still no support with this openHelperFactory?Impenetrable
F
16
  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
Farfetched answered 30/5, 2022 at 21:50 Comment(0)
Z
6

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.

Zygodactyl answered 15/7, 2021 at 11:57 Comment(0)
K
3

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.

Keyhole answered 16/3, 2021 at 13:30 Comment(0)
F
3

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

Forehand answered 26/11, 2021 at 16:13 Comment(1)
I was going to answer this when I saw it last in the list. Got my up vote.Ritualist
V
2

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

Database View

Valida answered 15/4, 2021 at 12:1 Comment(0)
E
2

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;
Expound answered 15/8, 2022 at 20:49 Comment(0)
Y
2

enter image description here

Just keep the app open and press Stop Inspectors in the devices list then select the new process for inspection, the database will be open.

Yeung answered 6/6, 2023 at 11:46 Comment(1)
Works on my machine!Transfusion
F
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.

Fornax answered 18/2, 2021 at 2:6 Comment(0)
A
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

Alegar answered 29/4, 2022 at 9:11 Comment(0)
D
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).

Dishrag answered 29/12, 2022 at 10:30 Comment(0)
A
1

The correct reason would be, You might be closing DB after insertion of data. Just do not close the DB and check.

Anabasis answered 5/9, 2023 at 10:11 Comment(0)
V
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

Viscus answered 14/12, 2020 at 15:18 Comment(0)
I
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

Insidious answered 11/3, 2021 at 19:28 Comment(0)
C
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.

Cartesian answered 2/9, 2021 at 10:48 Comment(0)
P
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.

Phonetic answered 24/1, 2023 at 14:25 Comment(0)
R
0

In my case I'm using Room for the database. I have 2 activities, MainActivity & DetailActivity. The activity that is using the database operation is DetailActivity

The database is not closed (open) when I opened the App Inspection when I'm opening DetailActivity, which contains the database operation

Hope this helps.

Reactance answered 3/4, 2023 at 17:44 Comment(0)
S
0

In my case, i stopped the inspector, and the reselect the device, it started working. Hope it helps somobedy.

enter image description here

Scandinavian answered 5/4, 2023 at 19:0 Comment(0)
H
0

I have discovered that I see the same issue when I didn't open co-routine with any DAO operations in the MainActivity file.

Hula answered 21/11, 2023 at 13:35 Comment(0)
W
0

in my case, I just close the database in my code

database.close();
Ween answered 25/11, 2023 at 21:53 Comment(0)
C
0

Open Android studio > Go to App Inspection > Database

then, click that power icon to and toggle to yellow lock that will open Connection and this will work

enter image description here

If this solved your issue don't forget to upvote :) Good day

Copland answered 14/1 at 9:37 Comment(0)
P
0

My solution was to downgrade the three dependencies associated with Room. It was at 2.6.something and then I downgraded to 2.5.2.

Pecan answered 26/1 at 1:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.