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?