Skip to content

Instantly share code, notes, and snippets.

@jayrambhia
Created April 8, 2015 12:24
Show Gist options
  • Save jayrambhia/aaa20788ec2704c2326f to your computer and use it in GitHub Desktop.
Save jayrambhia/aaa20788ec2704c2326f to your computer and use it in GitHub Desktop.
ORMLiteDemo
import android.content.Context;
import com.j256.ormlite.android.apptools.OpenHelperManager;
/**
* Created by Jay Rambhia on 05/04/15.
*/
public class DatabaseManager {
private DatabaseHelper databaseHelper = null;
public DatabaseHelper getHelper(Context context) {
if (databaseHelper == null) {
databaseHelper = OpenHelperManager.getHelper(context, DatabaseHelper.class);
}
return databaseHelper;
}
public void releaseHelper() {
if (databaseHelper != null) {
OpenHelperManager.releaseHelper();
databaseHelper = null;
}
}
}
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.RuntimeExceptionDao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
import java.sql.SQLException;
/**
* Created by Jay Rambhia on 05/04/15.
*/
public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
private static final String DATABASE_NAME = "ormlite_demo.db";
private static final int DATABASE_VERSION = 1;
private Dao<Note, Integer> noteDao = null;
private RuntimeExceptionDao<Note, Integer> noteRuntimeDao = null;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
try {
TableUtils.createTable(connectionSource, Note.class);
} catch (SQLException e) {
Log.e(DatabaseHelper.class.getName(), "can't create database", e);
throw new RuntimeException(e);
}
}
@Override
public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {
try {
TableUtils.dropTable(connectionSource, Note.class, true);
onCreate(database, connectionSource);
} catch (SQLException e) {
Log.e(DatabaseHelper.class.getName(), "Can't drop database", e);
throw new RuntimeException(e);
}
}
@Override
public void close() {
super.close();
noteDao = null;
}
public RuntimeExceptionDao<Note, Integer> getNoteDataDao() {
if (noteRuntimeDao == null) {
noteRuntimeDao = getRuntimeExceptionDao(Note.class);
}
return noteRuntimeDao;
}
public Dao<Note, Integer> getNoteDao() throws SQLException {
if (noteDao == null) {
noteDao = getDao(Note.class);
}
return noteDao;
}
}
import com.j256.ormlite.field.DataType;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;
import java.util.Date;
@DatabaseTable(tableName = "notes")
public class Note {
public static final String TIMESTAMP_FIELD = "timestamp";
@DatabaseField(generatedId = true, allowGeneratedIdInsert = true)
public int id;
@DatabaseField(useGetSet = true)
private String title;
@DatabaseField(canBeNull = false, useGetSet = true)
private String message;
@DatabaseField(dataType = DataType.DATE_LONG, canBeNull = false, useGetSet = true, columnName = TIMESTAMP_FIELD)
private Date created_ts;
// For UI Purposes
private boolean selected;
// Empty Constructor for ORMLite
public Note() {
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Date getCreated_ts() {
return created_ts;
}
public void setCreated_ts(Date created_ts) {
this.created_ts = created_ts;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
}
import android.content.Context;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.PreparedQuery;
import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.Where;
import java.sql.SQLException;
import java.util.List;
public class NoteRepository {
private DatabaseHelper dbHelper;
private Dao<Note, Integer> noteDao;
public NoteRepository(Context context) {
DatabaseManager dbManager = new DatabaseManager();
dbHelper = dbManager.getHelper(context);
try {
noteDao = dbHelper.getNoteDao();
} catch (SQLException e) {
e.printStackTrace();
}
}
public int create(Note note) {
try {
return noteDao.create(note);
// dbHelper.getDatab
} catch (SQLException e) {
// e.printStackTrace();
}
return 0;
}
public int update(Note note) {
try {
return noteDao.update(note);
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
public int delete(Note note) {
try {
return noteDao.delete(note);
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
public Note getById(int id) {
try {
return noteDao.queryForId(id);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public List<Note> findAll() {
try {
return noteDao.queryForAll();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public long getNumberOfNotes() {
QueryBuilder<Note, Integer> qb = noteDao.queryBuilder();
try {
return qb.countOf();
} catch (SQLException e) {
return -1;
}
}
public List<Note> getRecentNotes(long limit) {
if (limit < 1) {
limit = 10;
}
QueryBuilder<Note, Integer> qb = noteDao.queryBuilder();
try {
qb.orderBy(Note.TIMESTAMP_FIELD, false);
qb.limit(limit);
PreparedQuery<Note> preparedQuery = qb.prepare();
return noteDao.query(preparedQuery);
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment