Skip to content

Instantly share code, notes, and snippets.

@simoales
Last active September 26, 2016 08:08
Show Gist options
  • Save simoales/c5ac63bef38d5ddf2739ac18ca2afaa4 to your computer and use it in GitHub Desktop.
Save simoales/c5ac63bef38d5ddf2739ac18ca2afaa4 to your computer and use it in GitHub Desktop.
public class DatabaseHelper extends SQLiteOpenHelper{
//Constants for db name and version
private static final String DATABASE_NAME = "todos.db";
private static final int DATABASE_VERSION = 1;
//SQL to create tables
private static final String TABLE_CATEGORIES_CREATE=
"CREATE TABLE " + CategoriesEntry.TABLE_NAME + " (" +
CategoriesEntry._ID + " INTEGER PRIMARY KEY, " +
CategoriesEntry.COLUMN_DESCRIPTION + " TEXT " +
")";
private static final String TABLE_TODOS_CREATE =
"CREATE TABLE " + TodosEntry.TABLE_NAME + " (" +
TodosEntry._ID + " INTEGER PRIMARY KEY, " +
TodosEntry.COLUMN_TEXT + " TEXT, " +
TodosEntry.COLUMN_CREATED + " TEXT default CURRENT_TIMESTAMP, " +
TodosEntry.COLUMN_EXPIRED + " TEXT, " +
TodosEntry.COLUMN_DONE + " INTEGER, " +
TodosEntry.COLUMN_CATEGORY + " INTEGER, " +
" FOREIGN KEY("+ TodosEntry.COLUMN_CATEGORY + ") REFERENCES " + CategoriesEntry.TABLE_NAME
+ "(" + CategoriesEntry._ID +") " + ")";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(TABLE_CATEGORIES_CREATE);
db.execSQL(TABLE_TODOS_CREATE);
//seed(db);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TodosEntry.TABLE_NAME);
db.execSQL("DROP TABLE IF EXISTS " + CategoriesEntry.TABLE_NAME);
onCreate(db);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment