20

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.

5
  • It totally is. What problem are you facing? Commented Oct 21, 2011 at 11:46
  • Why don't you add tables instead of multiple DB? Commented 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.. Commented 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. Commented Oct 21, 2011 at 11:55
  • Any pro and con of using multiple databases. Commented Oct 16, 2020 at 6:54

2 Answers 2

6

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

Sign up to request clarification or add additional context in comments.

3 Comments

thanks for replying rihan.. Actully table attributes have to be genreated at runtime so there can be two tables with same name .So i need to create two db .
I don't think it is a better idea to have multiple databases you should go with creating multiple tables in a single database.for your problem tables of same name you can check and compare the names at run time they are sane you can give it a different name or append something in table name for example if your table name is mTable and user created mTable again you can give it a different name like mTable1.
sunny i cant apply your idea because table values will come from json
0

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:

  • database1
  • database2

And usually also

  • database1-journal
  • database2-journal

Any maybe another file for each database which contains unsaved (unmerged) changes to that database.

1 Comment

As this question pops up first on Google, I added an answer with example code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.