Skip to content

Instantly share code, notes, and snippets.

@markojerkic
Created April 22, 2017 08:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markojerkic/4a8fa4abe110a26bf3fdaa22144b5ebe to your computer and use it in GitHub Desktop.
Save markojerkic/4a8fa4abe110a26bf3fdaa22144b5ebe to your computer and use it in GitHub Desktop.
A sample code that shows how the SQLiteOpenHelper works
package com.markojerkic.gradetrack.subjectData;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class SubjectDbHelper extends SQLiteOpenHelper {
private static final String SQL_CREATE_ENTRIES =
"CREATE TABLE " + SubjectFeedReaderConctact.SubjectFeedEntry.TABLE_NAME + " (" + //Creating a table named in the FeedEntery class
SubjectFeedReaderConctact.SubjectFeedEntry._ID + " INTEGER PRIMARY KEY," + //_ID should always be INTEGER PRIMARY KEY
SubjectFeedReaderConctact.SubjectFeedEntry.COLUMN_SUBJECT + " TEXT," + //Creating a column named in the FeedEntery
SubjectFeedReaderConctact.SubjectFeedEntry.COLUMN_AVG_GRADE + " INTEGER)";//Creating a clumn named in the FeedEntery
//Most of the time ,you will not need to change these methods
//Just write them like this
private static final String SQL_DELETE_ENTRIES =
"DROP TABLE IF EXISTS " + SubjectFeedReaderConctact.SubjectFeedEntry.TABLE_NAME;
public static final int SUBJECTS_DATABASE_VERSION = 1;
public static final String SUBJECTS_DATABASE_NAME = "Subject.db";
public SubjectDbHelper(Context context) {
super(context, SUBJECTS_DATABASE_NAME, null, SUBJECTS_DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL(SQL_CREATE_ENTRIES);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL(SQL_DELETE_ENTRIES);
onCreate(sqLiteDatabase);
}
@Override
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
super.onDowngrade(db, oldVersion, newVersion);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment