条件数据库Androidsqllite的简单使用


当前第2页 返回上一页

        return mDb.query(distinct, tableName, columns, selection, selectionArgs, groupBy, having, orderBy, limit);
    }

    /**
     * 取得单行记录
     * @param tableName 表名
     * @param columns 获取的列数组
     * @param selection 条件
     * @param selectionArgs 条件中“?”对应的值
     * @param groupBy 分组
     * @param having 分组条件
     * @param orderBy 排序
     * @param limit 数据区间
     * @param distinct 是否去重复
     * @return
     * @throws SQLException
     */
    public Cursor findOne(boolean distinct, String tableName, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit) throws SQLException {

        Cursor mCursor = findList(distinct, tableName, columns, selection, selectionArgs, groupBy, having, orderBy, limit);

        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;

    }

    /**
     * 执行SQL(带参数)
     * @param sql
     * @param args SQL中“?”参数值
     */
    public void execSQL(String sql, Object[] args) {
        mDb.execSQL(sql, args);

    }

    /**
     * 执行SQL
     * @param sql
     */
    public void execSQL(String sql) {
        mDb.execSQL(sql);

    }

    /**
     * 判断某张表是否存在
     * @param tabName 表名
     * @return
     */
    public boolean isTableExist(String tableName) {
        boolean result = false;
        if (tableName == null) {
            return false;
        }

        try {
            Cursor cursor = null;
            String sql = "select count(1) as c from sqlite_master where type ='table' and name ='" + tableName.trim() + "'";
            cursor = mDb.rawQuery(sql, null);
            if (cursor.moveToNext()) {
                int count = cursor.getInt(0);
                if (count > 0) {
                    result = true;
                }
            }

            cursor.close();
        }
        catch (Exception e) {
        }
        return result;
    }

    /**
     * 判断某张表中是否存在某字段(注,该方法无法判断表是否存在,因此应与isTableExist一起应用)
     * @param tabName 表名
     * @param columnName 列名
     * @return
     */
    public boolean isColumnExist(String tableName, String columnName) {
        boolean result = false;
        if (tableName == null) {
            return false;
        }

        try {
            Cursor cursor = null;
            String sql = "select count(1) as c from sqlite_master where type ='table' and name ='" + tableName.trim() + "' and sql like '%" + columnName.trim() + "%'";
            cursor = mDb.rawQuery(sql, null);
            if (cursor.moveToNext()) {
                int count = cursor.getInt(0);
                if (count > 0) {
                    result = true;
                }
            }

            cursor.close();
        }
        catch (Exception e) {
        }
        return result;
    }

}
 


打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...