Two databases in an Android application

4

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

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

Is this possible on Android? Any examples that might help?

    
asked by anonymous 01.07.2014 / 16:26

1 answer

3

Yes, just give another name for your DB on your SqliteOpenHelper . SQLite is a file, so there may 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 file). As a good practice and easiness, create a SQLiteOpenHelper for each bank (file).

Official documentation: link

Tutorial: link

    
01.07.2014 / 16:39