Two databases in an Android app

Asked

Viewed 436 times

4

I have an Android application and this works with an internal database.

Now I need this application to work with 2 internal databases.

Is this possible on Android? Some example that can help?

1 answer

3


Yeah, just give another name to your DB in his SqliteOpenHelper. the SQLite is a file, so there can be as many as you want.

package com.example.stackoverflowsandbox;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class SampleHelpeMyDB1 extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "myDB1.sqlite";

    public SampleHelpeMyDB1( final Context context, final String name, final CursorFactory factory, final int version ) {
        super( context, SampleHelpeMyDB1.DATABASE_NAME, null, 1 );
    }

    @Override
    public void onCreate( final SQLiteDatabase db ) {
        // staff...
    }

    @Override
    public void onUpgrade( final SQLiteDatabase db, final int oldVersion, final int newVersion ) {
        // staff...
    }

}

In the example above, I am creating a DB called myDB1.sqlite (physical name of the SQLite). As good practice and ease, create a SQLiteOpenHelper for each bank (file).

Official documentation: http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html

Tutorial: http://www.vogella.com/tutorials/AndroidSQLite/article.html#databasetutorial

  • 1

    It would be nice to include some code snippet to facilitate understanding of this functionality.

  • Done @Wakim. Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.