LoiszadLoiszadLoiszad SQLite 移动端轻量级数据库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
* Created by zhaojy on 2016/9/2.
*/
public class {
private Context mContext;
public static CrossDBManager hpdbManager;
private CrossDataBaseHelper dataBaseHelper;
private SQLiteDatabase db;
private () {
super();
}
private (Context mContext) {
this.mContext = mContext;
dataBaseHelper = new CrossDataBaseHelper(mContext);
db = dataBaseHelper.getWritableDatabase();
}
public static CrossDBManager getInstance(Context context) {
if (hpdbManager == null) {
synchronized (CrossDBManager.class) {
hpdbManager = new CrossDBManager(context);
}
}
return hpdbManager;
}
* 增
*
* @param key
* @param value
* @return
*/
public boolean add(String key, String value) {
try {
if (db != null) {
db.execSQL("insert into " + CrossConfiguration.DBTABLE_NAME +"(key,value) values(?,?)",new String[]{key,value});
return true;
} else {
throw new Exception("Database init Failure");
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
* 删
*
* @param key
* @return
*/
public boolean delete(String key) {
try {
if (db != null) {
db.execSQL("delete from " + CrossConfiguration.DBTABLE_NAME + " where key=?",new String[]{key});
return true;
} else {
throw new Exception("Database init Failure");
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
* 改
*
* @param key
* @return
*/
public boolean update(String key, String newValue) {
try {
if (db != null) {
db.execSQL("update " + CrossConfiguration.DBTABLE_NAME + " set value=? where key=?",new String[]{newValue,key});
return true;
} else {
throw new Exception("Database init Failure");
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
* 查
*
* @param key
* @return 查询成功返回String 否则返回""
*/
public String query(String key) {
try {
if (db != null) {
Map<String, String> map = new HashMap<String, String>();
Cursor cursor = db.rawQuery("select value from " + CrossConfiguration.DBTABLE_NAME + " where key=?", new
String[]{key});
int cols_len = cursor.getColumnCount();
while (cursor.moveToNext()) {
for (int i = 0; i < cols_len; i++) {
String cols_name = cursor.getColumnName(i);
String cols_value = cursor.getString(cursor
.getColumnIndex(cols_name));
if (cols_value == null) {
cols_value = "";
}
map.put(cols_name, cols_value);
}
}
cursor.close();
return map.get("value");
} else {
throw new Exception("Database init Failure");
}
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
* close database
*/
// public void closeDB() {
// if (db != null) {
// db.close();
// }
// }
}