Skip to content

Instantly share code, notes, and snippets.

@horaceheaven
Created August 17, 2014 00:50
Show Gist options
  • Save horaceheaven/98cab03f4a1a6ff60cb6 to your computer and use it in GitHub Desktop.
Save horaceheaven/98cab03f4a1a6ff60cb6 to your computer and use it in GitHub Desktop.
Creating the OrmliteOpenHelper class
public class TodoOpenDatabaseHelper extends OrmLiteSqliteOpenHelper{
private static final String DATABASE_NAME = "todo";
private static final int DATABASE_VERSION = 1;
/**
* The data access object used to interact with the Sqlite database to do C.R.U.D operations.
*/
private Dao<Todo, Long> todoDao;
public TodoOpenDatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION,
/**
* R.raw.ormlite_config is a reference to the ormlite_config.txt file in the
* /res/raw/ directory of this project
* */
R.raw.ormlite_config);
}
@Override
public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
try {
/**
* creates the Todo database table
*/
TableUtils.createTable(connectionSource, Todo.class);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource,
int oldVersion, int newVersion) {
try {
/**
* Recreates the database when onUpgrade is called by the framework
*/
TableUtils.dropTable(connectionSource, Todo.class, false);
onCreate(database, connectionSource);
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* Returns an instance of the data access object
* @return
* @throws SQLException
*/
public Dao<Todo, Long> getDao() throws SQLException {
if(todoDao == null) {
todoDao = getDao(Todo.class);
}
return todoDao;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment