· 9 years ago · Dec 25, 2016, 07:42 AM
1public class DbHelper extends SQLiteOpenHelper {
2
3 //USER TABLE
4 public static final String USER_TABLE_NAME = "users";
5 public static final String USER_COLUMN_USER_ID = "id";
6 public static final String USER_COLUMN_USER_NAME = "usr_name";
7 public static final String USER_COLUMN_USER_ADDRESS = "usr_add";
8 public static final String USER_COLUMN_USER_CREATED_AT = "created_at";
9 public static final String USER_COLUMN_USER_UPDATED_AT = "updated_at";
10
11 @Inject
12 public DbHelper(@ApplicationContext Context context,
13 @DatabaseInfo String dbName,
14 @DatabaseInfo Integer version) {
15 super(context, dbName, null, version);
16 }
17
18 @Override
19 public void onCreate(SQLiteDatabase db) {
20 tableCreateStatements(db);
21 }
22
23 @Override
24 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
25 db.execSQL("DROP TABLE IF EXISTS " + USER_TABLE_NAME);
26 onCreate(db);
27 }
28
29 private void tableCreateStatements(SQLiteDatabase db) {
30 try {
31 db.execSQL(
32 "CREATE TABLE IF NOT EXISTS "
33 + USER_TABLE_NAME + "("
34 + USER_COLUMN_USER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
35 + USER_COLUMN_USER_NAME + " VARCHAR(20), "
36 + USER_COLUMN_USER_ADDRESS + " VARCHAR(50), "
37 + USER_COLUMN_USER_CREATED_AT + " VARCHAR(10) DEFAULT " + getCurrentTimeStamp() + ", "
38 + USER_COLUMN_USER_UPDATED_AT + " VARCHAR(10) DEFAULT " + getCurrentTimeStamp() + ")"
39 );
40
41 } catch (SQLException e) {
42 e.printStackTrace();
43 }
44 }
45
46 protected User getUser(Long userId) throws Resources.NotFoundException, NullPointerException {
47 Cursor cursor = null;
48 try {
49 SQLiteDatabase db = this.getReadableDatabase();
50 cursor = db.rawQuery(
51 "SELECT * FROM "
52 + USER_TABLE_NAME
53 + " WHERE "
54 + USER_COLUMN_USER_ID
55 + " = ? ",
56 new String[]{userId + ""});
57
58 if (cursor.getCount() > 0) {
59 cursor.moveToFirst();
60 User user = new User();
61 user.setId(cursor.getLong(cursor.getColumnIndex(USER_COLUMN_USER_ID)));
62 user.setName(cursor.getString(cursor.getColumnIndex(USER_COLUMN_USER_NAME)));
63 user.setAddress(cursor.getString(cursor.getColumnIndex(USER_COLUMN_USER_ADDRESS)));
64 user.setCreatedAt(cursor.getString(cursor.getColumnIndex(USER_COLUMN_USER_CREATED_AT)));
65 user.setUpdatedAt(cursor.getString(cursor.getColumnIndex(USER_COLUMN_USER_UPDATED_AT)));
66 return user;
67 } else {
68 throw new Resources.NotFoundException("User with id " + userId + " does not exists");
69 }
70 } catch (NullPointerException e) {
71 e.printStackTrace();
72 throw e;
73 } finally {
74 if (cursor != null)
75 cursor.close();
76 }
77 }
78
79 protected Long insertUser(User user) throws Exception {
80 try {
81 SQLiteDatabase db = this.getWritableDatabase();
82 ContentValues contentValues = new ContentValues();
83 contentValues.put(USER_COLUMN_USER_NAME, user.getName());
84 contentValues.put(USER_COLUMN_USER_ADDRESS, user.getAddress());
85 return db.insert(USER_TABLE_NAME, null, contentValues);
86 } catch (Exception e) {
87 e.printStackTrace();
88 throw e;
89 }
90 }
91
92 private String getCurrentTimeStamp() {
93 return String.valueOf(System.currentTimeMillis() / 1000);
94 }
95}