-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathDatabaseAdapter
281 lines (225 loc) · 7.22 KB
/
DatabaseAdapter
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import com.util.SqlUtils;
public class DatabaseAdapter {
public static String TAG = DatabaseAdapter.class.getSimpleName();
public static final String DB_NAME = "db.db"; // 数据库名称
private static final int version = 1; // 数据库版本
private static DatabaseHelper mDbHelper; //用来创建数据库
private static SQLiteDatabase mSqlLiteDb; //用来获得操作的数据库(读写)
private static DatabaseAdapter mInstance; //获得数据库实例,对数据库中的表进行操作
/**
* 不推荐调用
* @param ctx
*/
private DatabaseAdapter(Context ctx) {
//mDbHelper = new DatabaseHelper(SmartHomeApplication.getContext());
mDbHelper = new DatabaseHelper(ctx);
mSqlLiteDb = mDbHelper.getWritableDatabase();
}
/**
* 获取数据库操作适配器
* @return
*/
public static synchronized DatabaseAdapter getDbAdapter(){
if(mInstance == null){
mInstance = new DatabaseAdapter(BusinessDistrictApplication.getContext());
}
//防止返回后再次启动应用,数据库已经关闭;数据库没打开,则重新打开数据库
if(null==mDbHelper){
mDbHelper = new DatabaseHelper(BusinessDistrictApplication.getContext());
}
if(null == mSqlLiteDb || !mSqlLiteDb.isOpen()){
mSqlLiteDb = mDbHelper.getWritableDatabase();
}
return mInstance;
}
public static class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, version);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
//cleanDatabase();
Log.i("DatabaseAdapter_________===========", "run");
db.execSQL(UserEntry.TABLE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
/** open database */
public DatabaseAdapter open() throws SQLException {
// db = mDbHelper.getWritableDatabase();
return this;
}
/** close database */
public void close() {
// mDbHelper.close();
}
public void destroy(){
Log.i(TAG,TAG+" destroy!==========================================================================");
mDbHelper.close();
}
/** delete row by id */
public boolean delete(long rowId, String TABLE_NAME) {
int re = -1;
synchronized (mSqlLiteDb) {
re = mSqlLiteDb.delete(TABLE_NAME, "id" + "=" + rowId, null);
}
return re > 0;
}
/** delete row by id */
public boolean delete(String TABLE_NAME) {
int re = -1;
synchronized (mSqlLiteDb) {
re = mSqlLiteDb.delete(TABLE_NAME, null, null);
}
return re > 0;
}
/**
* 删除按指定排序条件排序后的前几条记录,使数据库表中的记录个数保持为maxCount条
* */
public boolean delete(String TABLE_NAME,String orderBy,int maxCount) {
if(maxCount<=0){
return false;
}
//查询数据库
Cursor cursor = mSqlLiteDb.query(TABLE_NAME,null,null,null,null,null,orderBy,null);
//数据库有记录
if(null != cursor && cursor.moveToFirst()){
//设置删除的个数
int count = cursor.getCount()-maxCount;
//开始删除
for(int i=0;i<count;i++){
delete((long)(cursor.getInt(cursor.getColumnIndex("id"))),TABLE_NAME);
cursor.moveToNext();
}
}
return true;
}
/** query all row */
public Cursor queryAll(String TABLE_NAME, String[] columns,String selection, String[] selectionArgs, String groupBy
,String having, String orderBy, String limit) {
Cursor cursor = mSqlLiteDb.query(TABLE_NAME, columns, selection, selectionArgs,groupBy, having, orderBy, limit);
return cursor;
}
/** query all row */
@SuppressWarnings({ "rawtypes"})
public List queryAll(Class<?> clazz, String TABLE_NAME, String[] columns,String selection, String[] selectionArgs, String groupBy
,String having, String orderBy, String limit) {
Cursor cursor = null;
List list = null;
try {
cursor = mSqlLiteDb.query(TABLE_NAME, columns, selection, selectionArgs,groupBy, having, orderBy, limit);
list = SqlUtils.cursor2PrivateVOList(cursor, clazz);
} catch (Exception e) {
e.printStackTrace();
System.out.println("ERROR @:queryAll");
} finally {
if(cursor != null){
cursor.close();
}
}
return list;
}
/** query row by id */
public Cursor queryById(Integer id, String TABLE_NAME, String[] columns) {
Cursor cursor = null;
cursor=mSqlLiteDb.query(TABLE_NAME, columns, "id=?",new String[] { id.toString() }, null, null, null);
return cursor;
}
/** query row by id */
public Class<?> queryById(Class<?> clazz, Integer id, String TABLE_NAME, String[] columns) {
Object obj = null;
Cursor cursor = null;
try {
cursor=mSqlLiteDb.query(TABLE_NAME, columns, "id=?",new String[] { id.toString() }, null, null, null);
obj = SqlUtils.cursor2PrivateVO(cursor, clazz);
}catch (Exception e) {
e.printStackTrace();
System.out.println("ERROR @:queryById");
} finally {
if(cursor != null){
cursor.close();
}
}
return (obj != null ? (Class<?>)obj : null);
}
/** insert data */
public long insert(String TABLE_NAME, ContentValues values) {
long re = -1;
re = mSqlLiteDb.insert(TABLE_NAME, null, values);
return re;
}
/** database is open state */
public boolean isOpen() {
if (null == mSqlLiteDb)
return false;
else
return mSqlLiteDb.isOpen();
}
/** delete data for where */
public boolean delete(String table, String whereClause, String[] whereArgs) {
int re = -1;
re = mSqlLiteDb.delete(table, whereClause, whereArgs);
return 0 < re;
}
public void beginTransaction() {
mSqlLiteDb.beginTransaction();
}
public void setTransactionSuccessful() {
mSqlLiteDb.setTransactionSuccessful();
}
public void endTransaction() {
mSqlLiteDb.endTransaction();
}
public void commitTransaction(){
setTransactionSuccessful();
endTransaction();
}
public void execSQL(String sql) {
mSqlLiteDb.execSQL(sql);
}
public Cursor querySQL(String sql,String [] selectionArgs) {
Cursor curson = null;
curson = mSqlLiteDb.rawQuery(sql, selectionArgs);
return curson;
}
/** update data */
public void update(String TABLE_NAME, Integer id, ContentValues values) {
mSqlLiteDb.update(TABLE_NAME, values, "id=?", new String[] { id.toString() });
}
//更新数据库表,指定条件
public void update(String TABLE_NAME, ContentValues values,String whereClause,String[] whereArgs ) {
mSqlLiteDb.update(TABLE_NAME, values, whereClause, whereArgs);
}
//获得数据库表
public static SQLiteDatabase getDatabase() {
getDbAdapter();
return mSqlLiteDb;
}
/** 清空表中的数据 */
public static boolean cleanTable(String TABLE_NAME) {
int re = -1;
re = mSqlLiteDb.delete(TABLE_NAME, null, null);
return re > 0;
}
//清空数据库中表的记录
public static void cleanDatabase(){
Log.i(TAG,TAG+" entry cleanDatabase");
if(null==mInstance){
getDbAdapter();
}
cleanTable(LoginEntry.TABLE_NAME);
}
}