-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.txt
183 lines (162 loc) · 6.23 KB
/
test.txt
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package com.lovethinking.passport.db;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.lovethinking.passport.constant.DbConstant;
import com.lovethinking.passport.util.MyLog;
abstract public class DatabaseHelper implements DbConstant {
private static final String TAG = DatabaseHelper.class.getSimpleName();
protected static SQLiteDatabase mDatabase;
public synchronized SQLiteDatabase openDatabase(Context context) {
if (null == mDatabase || !mDatabase.isOpen()) {
mDatabase = context.openOrCreateDatabase(DB_NAME,
Context.MODE_PRIVATE, null);
}
return mDatabase;
}
public synchronized void createTables(Context context, String createSQL) {
mDatabase = openDatabase(context);
synchronized (mDatabase) {
try {
mDatabase.execSQL(createSQL);
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}
}
public synchronized void dropTable(Context context, String tableName) {
mDatabase = openDatabase(context);
synchronized (mDatabase) {
mDatabase.execSQL("DROP TABLE IF EXISTS " + tableName);
}
}
public synchronized void deleteDatabase(Context context) {
context.deleteDatabase(DB_NAME);
}
public synchronized List<Map<String, Object>> executeQuery(Context context,
String sql) {
mDatabase = openDatabase(context);
synchronized (mDatabase) {
List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
Cursor cursor = mDatabase.rawQuery(sql, null);
Map<String, Object> resultMap = null;
int columnNum = cursor.getColumnCount();
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor
.moveToNext()) {
resultMap = new HashMap<String, Object>();
for (int i = 0; i < columnNum; i++) {
resultMap.put(cursor.getColumnName(i), cursor.getString(i));
}
resultList.add(resultMap);
}
cursor.close();
if (resultList != null && resultList.size() > 0)
return resultList;
else
return null;
}
}
public synchronized long save(Context context, String tableName,
Map<String, Object> saveMap) {
mDatabase = openDatabase(context);
synchronized (mDatabase) {
if (saveMap == null)
return -1;
ContentValues cv = new ContentValues();
for (String key : saveMap.keySet()) {
cv.put(key, saveMap.get(key).toString());
}
long ret = mDatabase.insert(tableName, null, cv);
if (-1 == ret) {
MyLog.e(TAG, "an error occurred when save a map");
MyLog.d(TAG, "Look : " + saveMap.toString());
} else {
MyLog.d(TAG, "save a map success");
}
return ret;
}
}
public synchronized int update(Context context, String tableName,
Map<String, Object> saveMap, String whereClause) {
mDatabase = openDatabase(context);
synchronized (mDatabase) {
ContentValues cv = new ContentValues();
for (String key : saveMap.keySet()) {
cv.put(key, saveMap.get(key).toString());
}
int ret = mDatabase.update(tableName, cv, whereClause, null);
MyLog.d(TAG, "update " + tableName + saveMap.toString()
+ whereClause);
MyLog.d(TAG, ret + " rows affected when update");
return ret;
}
}
public synchronized void save(Context context, String tableName,
List<Map<String, Object>> saveList) {
mDatabase = openDatabase(context);
synchronized (mDatabase) {
mDatabase.beginTransaction();
if (null == saveList) {
saveList = new ArrayList<Map<String, Object>>();
}
ContentValues cv = null;
long ret = 0;
for (Map<String, Object> saveMap : saveList) {
cv = new ContentValues();
for (String key : saveMap.keySet()) {
cv.put(key, saveMap.get(key).toString());
}
ret = mDatabase.insert(tableName, null, cv);
if (-1 == ret) {
MyLog.e(TAG, "an error occurred when save a map");
MyLog.d(TAG, "Look : " + saveMap.toString());
} else {
MyLog.d(TAG, "save a map success");
}
}
if (ret >= 0) {
}
mDatabase.setTransactionSuccessful();
mDatabase.endTransaction();
}
}
public synchronized void delete(Context context, String tableName,
String clause) {
mDatabase = openDatabase(context);
synchronized (mDatabase) {
try {
if (clause == null || clause == "")
mDatabase.execSQL("DELETE FROM " + tableName);
else
mDatabase.execSQL("DELETE FROM " + tableName + " WHERE "
+ clause);
} catch (Exception e) {
MyLog.e(TAG, "error occurred when delete from " + tableName
+ " success");
MyLog.d(TAG, "Look : DELETE FROM " + tableName + " WHERE "
+ clause);
e.printStackTrace();
} finally {
MyLog.d(TAG, "delete from " + tableName + " success");
}
}
}
public synchronized void executeSQL(Context context, String sql) {
mDatabase = openDatabase(context);
synchronized (mDatabase) {
mDatabase.execSQL(sql);
}
}
public synchronized void closeDatabase() {
if (null != mDatabase) {
mDatabase.close();
mDatabase = null;
}
}
}