I am working on app in which I would like to use two sqlite database in single app. Is this possible? Any help will be appreciated.
-
It totally is. What problem are you facing?Vladimir– Vladimir2011-10-21 11:46:00 +00:00Commented Oct 21, 2011 at 11:46
-
Why don't you add tables instead of multiple DB?Vinay– Vinay2011-10-21 11:48:35 +00:00Commented Oct 21, 2011 at 11:48
-
Vladimir , i am new to android means i am beginer so can u please help me with any sample code or example in which we can use two database for single app .Actually i know how to use single database but not how to use two database in single app n moreover i want to add attributes in database at runtime also..user1006506– user10065062011-10-21 11:53:10 +00:00Commented Oct 21, 2011 at 11:53
-
Vinay , Thanks for your concern .. but actually it is requirement of my application to have two db and attributes of tables inside db should be genreated at runtime.user1006506– user10065062011-10-21 11:55:15 +00:00Commented Oct 21, 2011 at 11:55
-
Any pro and con of using multiple databases.Shubham AgaRwal– Shubham AgaRwal2020-10-16 06:54:19 +00:00Commented Oct 16, 2020 at 6:54
2 Answers
It is possible to have multiple databases in a single app. If you can do without multiple databases, you can have just a single database with multiple tables. You can use adb to view the databases and execute queries against them
3 Comments
Yes, you can have multiple SQLite databases.
A SQLite database is just a file.
You set the name the file (database) with the databaseName:
public class DatabaseHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
public DatabaseHelper(Context context, String databaseName) {
super(context, /*->*/ databaseName /*<-*/, null, DATABASE_VERSION);
}
}
To create multiple databases, use different database names:
final var database1 = new DatabaseHelper(context, "database1");
final var database2 = new DatabaseHelper(context, "database2");
Just fyi:
You can see your databases when you install your app on a device, connect the device via Android Studio (adb), open Device File Explorer, browser to data/data/com.your.app/databases
In the example above you'll find at least two files:
database1database2
And usually also
database1-journaldatabase2-journal
Any maybe another file for each database which contains unsaved (unmerged) changes to that database.