Reputation: 1364
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?
Upvotes: 62
Views: 46665
Reputation: 1047
What worked for me is selecting the app on the phone Developer Options > Select debug app > my app.
Upvotes: 0
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
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
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
If this solved your issue don't forget to upvote :) Good day
Upvotes: 1
Reputation: 46
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
Reputation: 1
in my case, I just close the database in my code
database.close();
Upvotes: 0
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
Reputation: 26452
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
Reputation: 1090
In my case, i stopped the inspector, and the reselect the device, it started working. Hope it helps somobedy.
Upvotes: 0
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
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
Reputation: 831
Weird but this is what worked for me:
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
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
Reputation: 749
Upvotes: 17
Reputation: 69
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
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:
Upvotes: 4
Reputation: 2295
Not sure if this will help, but there is a chance you have selected the wrong emulator/device for database inspector.
You can change the emulator for your database inspector by clicking on the name of the emulator as shown in the image.
Upvotes: 1
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
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
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
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
Reputation: 1056
UPD. last time I had this annoying issue Invalidate cache/Restart
fixed this for me. Sigh
Running the app in Debug mode (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:
In Debug mode (what I want):
Noticeable, that it continuing to work (querying the database etc) even after I stopped the app running in Debug mode:
Upvotes: 29
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
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