· 7 years ago · Jan 24, 2019, 02:52 PM
1public final class UserTable implements BaseColumns {
2
3
4 public static final String TABLE_NAME = "users";
5 public static final String COLUMN_NAME_EMAIL = "email";
6 public static final String COLUMN_NAME_SERVER_TOKEN = "token";
7
8
9 public static final String SQL_CREATE_TABLE =
10 "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (" +
11 _ID + " INTEGER PRIMARY KEY," +
12 COLUMN_NAME_EMAIL + " TEXT," +
13 COLUMN_NAME_SERVER_TOKEN + " TEXT DEFAULT 'pub'," +
14 "UNIQUE(" + COLUMN_NAME_EMAIL + ")" +
15 ");";
16}
17
18public class DBHelper extends SQLiteOpenHelper {
19
20private SQLiteDatabase myDB;
21
22public static final int DATABASE_VERSION = 2;
23public static final String DATABASE_NAME = "MDB.db";
24
25public static final String NO_MATCH = "NO_MATCH_WAS_FOUND";
26
27public DBHelper(Context context) {
28 super(context, DATABASE_NAME, null, DATABASE_VERSION);
29 context.openOrCreateDatabase(DATABASE_NAME, Context.MODE_PRIVATE, null);
30 create();
31}
32
33public void create() {
34 myDB = getWritableDatabase();
35 myDB.execSQL(DB.UserTable.SQL_CREATE_TABLE);
36 myDB.execSQL(DB.AccountTable.SQL_CREATE_TABLE);
37 close();
38 Log.d("mdatabase", "create");
39}
40
41public void onCreate(SQLiteDatabase db) {
42 db.execSQL(DB.UserTable.SQL_CREATE_TABLE);
43 db.execSQL(DB.AccountTable.SQL_CREATE_TABLE);
44 Log.d("mdatabase", "on create");
45}
46
47public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
48 // This database is only a cache for online data, so its upgrade policy is
49 // to simply to discard the data and start over
50 //db.execSQL(SQL_DELETE_ENTRIES);
51 //onCreate(db);
52}
53
54public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
55 onUpgrade(db, oldVersion, newVersion);
56}
57
58
59public long insert(String tableName, ContentValues values) {
60 Log.d("mdatabase", "insert " + tableName + ":" + values.toString());
61 open();
62 long res = myDB.insert(tableName, null, values);
63 close();
64 return res;
65}
66
67public int delete(String tableName, String whereClause) {
68 open();
69 int res = myDB.delete(tableName, whereClause, null);
70 close();
71 return res;
72}
73
74public int update(String tableName, ContentValues values, String whereClause) {
75 Log.d("mdatabase", "update " + tableName + ":" + values.toString());
76 open();
77 int res = myDB.update(tableName, values, whereClause, null);
78 close();
79 return res;
80}
81
82
83public String select(String tableName, String select, String whereClause) {
84 Log.d("mdatabase", "select " + tableName + ":" + select);
85 open();
86 Cursor c = myDB.rawQuery("SELECT " + select + " FROM " + tableName + " WHERE " + whereClause, null);
87 Log.d("mdatabase", "select= " + select);
88 if (c.moveToFirst()) {
89 Log.d("mdatabase", "there is a returned row");
90 return c.getString(c.getColumnIndexOrThrow(select));//String only?
91 }
92 c.close();
93 close();
94 return NO_MATCH;
95}
96
97public Map<String, String> selects(String tableName, String[] selects, String whereClause) {
98 Log.d("mdatabase", "selects " + tableName + ":" + selects.toString());
99 open();
100 Cursor c = myDB.rawQuery("SELECT * FROM " + tableName + " WHERE " + whereClause, null);
101 c.moveToNext();
102 // Neeeeds Do while!
103 Map<String, String> map = new HashMap<String, String>();
104 for (String column : selects) {
105 map.put(column, c.getString(c.getColumnIndex(column)));
106 }
107 c.close();
108 close();
109 return map;
110}
111
112public void open() {
113 myDB = getWritableDatabase();
114}
115
116public void close() {
117 myDB.close();
118}
119
120}
121
122public long setToken(String token) {
123 return mDB.update(DB.UserTable.TABLE_NAME,
124 new CVBuilder().add(DB.UserTable.COLUMN_NAME_SERVER_TOKEN, token).get(),
125 DB.UserTable.COLUMN_NAME_EMAIL + "=" + email);
126 }
127
128 public String getToken() {
129 return mDB.select(DB.UserTable.TABLE_NAME,
130 DB.UserTable.COLUMN_NAME_SERVER_TOKEN,
131 DB.UserTable.COLUMN_NAME_EMAIL + "=" + email);
132 }