Bouss
Bouss

Reputation: 205

Create SQLite database + table and save them for future use

I followed a tutorial to create an SQLite database and create tables in it for an application. But all of this happens at runtime. Now I'm looking for a way to create the database and table so I can re-use them in the future. So the same table will be updated with data that is inserted in the app. The database and table will only be created once.

This the code I'm using to create the database and tabel at runtime:

SQLiteHelperCourse courseDbHelper = new SQLiteHelperCourse(this);
    SQLiteDatabase dbw = courseDbHelper.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(CourseEntry.COLUMN_NAME_COURSE_ID, "1");
    values.put(CourseEntry.COLUMN_NAME_COURSE_NAME, "Android");
    values.put(CourseEntry.COLUMN_NAME_CREDITS, "3");
    values.put(CourseEntry.COLUMN_NAME_SEMESTER, "1st");
    values.put(CourseEntry.COLUMN_NAME_YEAR, "3rd");
    values.put(CourseEntry.COLUMN_NAME_COURSE_ID, "2");
    values.put(CourseEntry.COLUMN_NAME_COURSE_NAME, "SBP");
    values.put(CourseEntry.COLUMN_NAME_CREDITS, "3");
    values.put(CourseEntry.COLUMN_NAME_SEMESTER, "1st");
    values.put(CourseEntry.COLUMN_NAME_YEAR, "3rd");
    values.put(CourseEntry.COLUMN_NAME_COURSE_ID, "3");
    values.put(CourseEntry.COLUMN_NAME_COURSE_NAME, "IT Infrastructure");
    values.put(CourseEntry.COLUMN_NAME_CREDITS, "3");
    values.put(CourseEntry.COLUMN_NAME_SEMESTER, "1st");
    values.put(CourseEntry.COLUMN_NAME_YEAR, "3rd");
    dbw.insert(CourseEntry.TABLE_NAME, CourseEntry.COLUMN_NAME_NULLABLE, values);

    SQLiteDatabase dbr = courseDbHelper.getReadableDatabase();
    String[] projection = { CourseEntry.COLUMN_NAME_COURSE_NAME};
    final ArrayList<String> list = new ArrayList<String>();

    Cursor curs = dbr.query(CourseEntry.TABLE_NAME, 
            projection, 
            null, 
            null, 
            null, 
            null, 
            null);
    curs.moveToFirst();

    while(curs.moveToNext())
    {
        list.add(curs.getString(curs.getColumnIndexOrThrow(CourseEntry.COLUMN_NAME_COURSE_NAME)));
    }

EDIT: The code I provided is being called in the onCreate event of the activity.

Here is my SQLiteHelper class code:

public class SQLiteHelperCourse extends SQLiteOpenHelper {

public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "AndroidApplication.db";

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

private static final String TEXT_TYPE = " TEXT";
private static final String COMMA_SEP = ", ";
private static final String SQL_CREATE_ENTRIES = 
        "CREATE TABLE " + CourseEntry.TABLE_NAME + " (" + CourseEntry._ID + " INTEGER PRIMARY KEY, "
                                                        + CourseEntry.COLUMN_NAME_COURSE_ID + TEXT_TYPE + COMMA_SEP
                                                        + CourseEntry.COLUMN_NAME_COURSE_NAME + TEXT_TYPE + COMMA_SEP
                                                        + CourseEntry.COLUMN_NAME_YEAR + TEXT_TYPE + COMMA_SEP
                                                        + CourseEntry.COLUMN_NAME_SEMESTER + TEXT_TYPE + COMMA_SEP
                                                        + CourseEntry.COLUMN_NAME_CREDITS + TEXT_TYPE + ")";

private static final String SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS " + CourseEntry.TABLE_NAME;

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(SQL_CREATE_ENTRIES);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL(SQL_DELETE_ENTRIES);
    onCreate(db);
}

public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    onUpgrade(db, oldVersion, newVersion);
}

}

Upvotes: 0

Views: 156

Answers (1)

laalto
laalto

Reputation: 152927

Assuming you want to insert your three courses to the database only once when the database is created:

  • Move the insert code to database helper onCreate(). It gets called once when the database file didn't exist to start with. Use the SQLiteDatabase supplied as an argument there instead of calling getWritableDatabase() recursively.

  • Insert three times. ContentValues put() overwrites any value with the same key so essentially you're only really inserting your third course.

Upvotes: 1

Related Questions