0

In my Android app I want to use a SQLite database which I already created with a different program. I am not planning on writing into the database in the app. When I looked into the documentation I could only find instructions on how to create the database in the app after it is installed and then use it.

I then found something called SQLiteOpenHelper which seems like it would do that so I tried to use it.

public class DatabaseHelper extends SQLiteOpenHelper {
    private static int DATABASE_VERSION = 1;

    public static final String DATABASE_NAME = "ENUMBERS";
    public static final String TABLE_NAME = "MAIN";

    public static final String KEY_ROWID = "_ID";
    public static final String COLUMN_NAME_ENUMBER = "ENUMBER";
    public static final String COLUMN_NAME_NAME = "NAME";
    public static final String COLUMN_NAME_INFO = "INFO";
    public static final String COLUMN_NAME_STATUS = "STATUS";

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_MAIN_TABLE = "CREATE TABLE" + TABLE_NAME + "(" + KEY_ROWID + " INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, "
                + COLUMN_NAME_ENUMBER + " TEXT NOT NULL UNIQUE, "
                + COLUMN_NAME_NAME + " TEXT, "
                + COLUMN_NAME_INFO + " TEXT, "
                + COLUMN_NAME_STATUS + " TEXT) ";

        db.execSQL(CREATE_MAIN_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop old table if there is one
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);

        // Create new table
        onCreate(db);
    }
}

I am still not really sure how to do this now. How do I import my .db file with this and open it? Do I just have to create an instance of the class in the MainActivity and then call onCreate()? But how would I make a SPLiteDatabase class for that .db file?

niklassc
  • 597
  • 1
  • 9
  • 24
  • 2
    https://github.com/jgilfelt/android-sqlite-asset-helper – CommonsWare Oct 30 '17 at 13:24
  • 1
    you want to create a new version of database and keep old data? – A Farmanbar Oct 30 '17 at 13:27
  • @Mr.AF I have created the database using a different program and I want to somehow include that database in my app so that I can use it there with the same data that I have already added. – niklassc Oct 30 '17 at 13:30
  • i wasn't right into it before but as i know you must add permission in the first program that its database be in access for other programs in the manifest otherwise your next program wouldn't go through it . – A Farmanbar Oct 30 '17 at 13:32
  • This code creates a new `ENUMBERS.db`, by the way – OneCricketeer Oct 30 '17 at 13:32

0 Answers0