Home open multiple databases at the same time
Reply: 0

open multiple databases at the same time

Wang SL
1#
Wang SL Published in 2018-02-14 08:52:13Z

I am new to Android but I would like to do work with Android database. After some study and testing, I face the above problem. My set up environment: Single Android application with multiple threads to access 2 databases. I use Singleton Pattern to design SQliteOpenHelper for both databases. When I open and use the 1st database, it's ok. But when I open and use the 2nd database, some error occurs like "SQLiteException: no such table:","database is previously opened here..." I realize 2nd database is not really connected and still, the 1st database is connected to execute 2nd database statement which will lead to an error.

Here is my thinking. I know Android database should not be open and close too many times, this will lead to memory leak. The best way is to open database once and keep it connected all the time together with the application. But I do know how to keep 2 databases open at same time. When I create two SQliteOpenHelper by passing the same context (calling getApplicationContext()), I guess this will lead to 2nd database connection failure (since 1st db using this context has already connected a database).

So anyone can teach me how to keep 2 databases open and connected to the Android application at the same time? How to pass two different contexts? Must I close database every time use it? but I guess this approach will lead to memory leak issue.

Many Thanks Wang SL

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.41251 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO