DBManager

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> queryMutiResultBySQL(String sql,
        String[] selectionArgs) {
    List> list = new ArrayList>();
    Cursor cursor = database.rawQuery(sql, selectionArgs);
    int cols_len = cursor.getColumnCount();
    while (cursor.moveToNext()) {
        Map map = new HashMap();
        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);
        }
        list.add(map);
    }
    return list;
}

public Cursor queryMutiCursor(String sql, String[] selectionArgs) {
    Cursor cursor = database.rawQuery(sql, selectionArgs);
    return cursor;
}

/**************************** 以下是不需要sql语句的查询方法 **********************************/

/**
 * 
 * @param tabeName
 * @param nullColumnHack
 * @param values
 * @return
 */
public boolean insertByNotSQL(String tabeName, String nullColumnHack,
        ContentValues values) {
    boolean flag = false;
    // insert into tableName(a,,b,c) values(?,?,?)
    long id = database.insert(tabeName, nullColumnHack, values);
    flag = (id > 0 ? true : false);
    return flag;
}

/**
 * 无sql语句的更新操作
 * 
 * @param tabeName
 * @param values
 * @param whereClause
 * @param whereArgs
 * @return
 */
public boolean updateByNotSQL(String tabeName, ContentValues values,
        String whereClause, String[] whereArgs) {
    boolean flag = false;

    // update tableName set name =?,address=?,age=? where pid=?
    int count = database.update(tabeName, values, whereClause, whereArgs); // 影响数据的行数
    flag = (count > 0 ? true : false);

    return flag;
}

/**
 * 无sql语句的删除操作
 * 
 * @param table
 * @param whereClause
 * @param whereArgs
 * @return
 */
public boolean deleteNotSQL(String table, String whereClause,
        String[] whereArgs) {
    boolean flag = false;

    // delete from tableName where pid=?
    int count = database.delete(table, whereClause, whereArgs);
    flag = (count > 0 ? true : false);

    return flag;
}

/**
 * sql标准写法:select [distinct][columnName] ... from tableName
 * [where][selection][selectionArgs][groupBy][having][order by][limit]
 * 
 * @param distinct
 * @param table
 * @param columns
 * @param selection
 * @param selectionArgs
 * @param groupBy
 * @param having
 * @param orderBy
 * @param limit
 * @return
 */
public Cursor queryByNotSQL(boolean distinct, String table,
        String[] columns, String selection, String[] selectionArgs,
        String groupBy, String having, String orderBy, String limit) {
    Cursor cursor = null;

    /*
     * 这是一条折中 的查询方法,其他的方法就是一系列的构造函数
     */
    cursor = database.query(distinct, table, columns, selection,
            selectionArgs, groupBy, having, orderBy, limit);

    return cursor;
}
}

你可能感兴趣的:(DBManager)