public class DBManager {
private DBHelper dbHelper;
private SQLiteDatabase database;
public DBManager(Context context) {
dbHelper = new DBHelper(context);
}
/*
* 用于更新数据的包括插入、删除、修改
*/
public boolean updateBySQL(String sql, Object[] bindArgs) {
boolean flag = false;
try {
database.execSQL(sql, bindArgs);
flag = true;
} catch (SQLException e) {
e.printStackTrace();
} /*
* finally { if (database != null) { database.close(); } }
*/
return flag;
}
/*
* 单独一个方法,获得数据库的链接
*/
public void getDataBaseConn() {
database = dbHelper.getWritableDatabase();
}
/*
* 释放数据库链接
*/
public void releaseConn() {
if (database != null) {
database.close();
}
}
/*
* 查找数据的测试程序
*/
public void queryBySQL(String sql, String[] selectionArgs) {
Cursor cursor = database.rawQuery(sql, selectionArgs);
while (cursor.moveToNext()) {
System.out.println("-->>"
+ cursor.getString(cursor.getColumnIndex("name")));
System.out.println("-->>"
+ cursor.getString(cursor.getColumnIndex("address")));
System.out.println("-->>"
+ cursor.getInt(cursor.getColumnIndex("age")));
}
}
/*
* 查找单条数据的查询方法
*/
public Map querySingleResultBySQL(String sql,
String[] selectionArgs) {
Map map = new HashMap();
Cursor cursor = database.rawQuery(sql, selectionArgs);
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);
}
}
return map;
}
/**
* 同构反射获得数据库的记录; 声明Class的属性必须都是String类型
*
* @param sql
* @param selectionArgs
* @param cls
* @return
*/
public T querySingleCursor(String sql, String[] selectionArgs,
Class cls) {
T t = null;
Cursor cursor = database.rawQuery(sql, selectionArgs);
int cols_len = cursor.getColumnCount();
while (cursor.moveToNext()) {
try {
t = cls.newInstance();
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 = "";
}
Field field = null;
try {
field = cls.getDeclaredField(cols_name);
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
field.setAccessible(true);
field.set(t, cols_value);
}
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
return t;
}
/**
* 同构反射获得多条数据库的记录; 声明Class的属性必须都是String类型
*
* @param sql
* @param selectionArgs
* @param cls
* @return
*/
public List queryMutilCursor(String sql, String[] selectionArgs,
Class cls) {
List list = new ArrayList();
Cursor cursor = database.rawQuery(sql, selectionArgs);
int cols_len = cursor.getColumnCount();
while (cursor.moveToNext()) {
try {
T t = cls.newInstance();
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 = "";
}
Field field = null;
try {
field = cls.getDeclaredField(cols_name);
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
field.setAccessible(true);
field.set(t, cols_value);
list.add(t);
}
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
return list;
}
/*
* 查找多条数据的查询方法
*/
public List