Skip to content

Instantly share code, notes, and snippets.

@danleyb2
Forked from janishar/dagger2-demo-DbHelper.java
Created August 23, 2017 22:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danleyb2/3343072d97be8cdaaf7ea9076c6e7d37 to your computer and use it in GitHub Desktop.
Save danleyb2/3343072d97be8cdaaf7ea9076c6e7d37 to your computer and use it in GitHub Desktop.
@Singleton
public class DbHelper extends SQLiteOpenHelper {
//USER TABLE
public static final String USER_TABLE_NAME = "users";
public static final String USER_COLUMN_USER_ID = "id";
public static final String USER_COLUMN_USER_NAME = "usr_name";
public static final String USER_COLUMN_USER_ADDRESS = "usr_add";
public static final String USER_COLUMN_USER_CREATED_AT = "created_at";
public static final String USER_COLUMN_USER_UPDATED_AT = "updated_at";
@Inject
public DbHelper(@ApplicationContext Context context,
@DatabaseInfo String dbName,
@DatabaseInfo Integer version) {
super(context, dbName, null, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
tableCreateStatements(db);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + USER_TABLE_NAME);
onCreate(db);
}
private void tableCreateStatements(SQLiteDatabase db) {
try {
db.execSQL(
"CREATE TABLE IF NOT EXISTS "
+ USER_TABLE_NAME + "("
+ USER_COLUMN_USER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ USER_COLUMN_USER_NAME + " VARCHAR(20), "
+ USER_COLUMN_USER_ADDRESS + " VARCHAR(50), "
+ USER_COLUMN_USER_CREATED_AT + " VARCHAR(10) DEFAULT " + getCurrentTimeStamp() + ", "
+ USER_COLUMN_USER_UPDATED_AT + " VARCHAR(10) DEFAULT " + getCurrentTimeStamp() + ")"
);
} catch (SQLException e) {
e.printStackTrace();
}
}
protected User getUser(Long userId) throws Resources.NotFoundException, NullPointerException {
Cursor cursor = null;
try {
SQLiteDatabase db = this.getReadableDatabase();
cursor = db.rawQuery(
"SELECT * FROM "
+ USER_TABLE_NAME
+ " WHERE "
+ USER_COLUMN_USER_ID
+ " = ? ",
new String[]{userId + ""});
if (cursor.getCount() > 0) {
cursor.moveToFirst();
User user = new User();
user.setId(cursor.getLong(cursor.getColumnIndex(USER_COLUMN_USER_ID)));
user.setName(cursor.getString(cursor.getColumnIndex(USER_COLUMN_USER_NAME)));
user.setAddress(cursor.getString(cursor.getColumnIndex(USER_COLUMN_USER_ADDRESS)));
user.setCreatedAt(cursor.getString(cursor.getColumnIndex(USER_COLUMN_USER_CREATED_AT)));
user.setUpdatedAt(cursor.getString(cursor.getColumnIndex(USER_COLUMN_USER_UPDATED_AT)));
return user;
} else {
throw new Resources.NotFoundException("User with id " + userId + " does not exists");
}
} catch (NullPointerException e) {
e.printStackTrace();
throw e;
} finally {
if (cursor != null)
cursor.close();
}
}
protected Long insertUser(User user) throws Exception {
try {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(USER_COLUMN_USER_NAME, user.getName());
contentValues.put(USER_COLUMN_USER_ADDRESS, user.getAddress());
return db.insert(USER_TABLE_NAME, null, contentValues);
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
private String getCurrentTimeStamp() {
return String.valueOf(System.currentTimeMillis() / 1000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment