'Android Studio Database Inspector always showing database as "closed"
Solution 1:[1]
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:
Solution 2:[2]
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
Solution 3:[3]
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.
Solution 4:[4]
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.
Solution 5:[5]
Enable "keep database connection open". See the image below
Solution 6:[6]
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.
Solution 7:[7]
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:
Solution 8:[8]
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
Solution 9:[9]
Solution 10:[10]
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
Solution 11:[11]
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
Solution 12:[12]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow