Skip to content

Multiple Database Connections

Siim Kinks edited this page Jan 21, 2018 · 2 revisions

By default, all database operations and queries are run on the default database connection initialized with the SqliteMagic.builder(Application).[..].openDefaultConnection() builder. SqliteMagic also supports multiple connections to differently named databases (with the same schema).

To open a new database connection use the connection builder:

DbConnection newConnection = SqliteMagic
    .builder(applicationContext)
    .sqliteFactory(new FrameworkSQLiteOpenHelperFactory())
    .name("db_nr_2.db")
    .openNewConnection();

This call creates a new database and its schema with name "db_nr_2.db" if needed, executes any migrations if needed and returns the connection object.

To execute operations or queries on the opened connection just call usingConnection(DbConnection) on any of the operation builders:

long id = author
    .insert()
    .usingConnection(dbConnection)
    .execute();
    
List<Author> authors = Select
    .from(AUTHOR)
    .usingConnection(dbConnection)
    .execute();

Clone this wiki locally