Wamiq
Wamiq

Reputation: 1364

Android Studio Database Inspector always showing database as "closed"

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

Upvotes: 62

Views: 46665

Answers (25)

Amir
Amir

Reputation: 1047

What worked for me is selecting the app on the phone Developer Options > Select debug app > my app.

Upvotes: 0

Pitam Poudel
Pitam Poudel

Reputation: 61

I know this question already has an accepted answer but I encountered the same issue where the Room database appeared as "closed" in Android Studio's Database Inspector, albeit for a different reason. In my case, it was due to using a bundled SQLite driver in a Kotlin Multiplatform (KMP) project.

Switching to the platform-specific AndroidSqliteDriver for the Android target resolved the issue. If you're experiencing this problem, ensure you're using the correct platform-specific driver to work seamlessly with Android Studio's tools.

Upvotes: 6

Edward Kim
Edward Kim

Reputation: 1

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.

Upvotes: 0

Prawesh Panthi
Prawesh Panthi

Reputation: 358

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

Upvotes: 1

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

Upvotes: 1

Jamel Miraoui
Jamel Miraoui

Reputation: 1

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

database.close();

Upvotes: 0

Niraj
Niraj

Reputation: 41

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

Upvotes: 1

All Іѕ Vаиітy
All Іѕ Vаиітy

Reputation: 26452

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.

Upvotes: 8

Azamat Mahkamov
Azamat Mahkamov

Reputation: 1090

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

enter image description here

Upvotes: 0

Rickyslash
Rickyslash

Reputation: 451

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.

Upvotes: 0

Vad4mus
Vad4mus

Reputation: 1

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.

Upvotes: 0

WebViewer
WebViewer

Reputation: 831

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).

Upvotes: 1

Akhil Achary
Akhil Achary

Reputation: 23

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;

Upvotes: 2

ItSNeverLate
ItSNeverLate

Reputation: 749

  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

Upvotes: 17

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

Upvotes: 1

flyingfishcattle
flyingfishcattle

Reputation: 2153

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

Upvotes: 4

Abhi
Abhi

Reputation: 2295

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.

Upvotes: 1

Swathi
Swathi

Reputation: 1684

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.

Upvotes: 0

tiz
tiz

Reputation: 106

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.

Upvotes: 7

Jim
Jim

Reputation: 39

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

Database View

Upvotes: 2

Yazan Al-Trabulsi
Yazan Al-Trabulsi

Reputation: 31

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.

Upvotes: 3

fhomovc
fhomovc

Reputation: 301

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

Upvotes: 0

exenza
exenza

Reputation: 1056

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

Upvotes: 29

IshRoid
IshRoid

Reputation: 3735

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

Upvotes: 19

Crag
Crag

Reputation: 1851

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

Upvotes: 0

Related Questions