public Cursor query (String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)
Query the given table, returning a Cursor over the result set.

Parameters
String table  -----------   The table name to compile the query against. 哪个表 table,要查询的哪个表.
String[] columns--------- A list of which columns to return. 返回哪一列,如果参数是null,则返回所有列(不鼓励设置为null,以免防止读出的数据没有用到)(举例见selectionArgs)

String selection---------返回哪一行的过滤器,格式是SQL的WHERE,设置为null,返回这个table的所有行.
                A filter declaring which rows to return, formatted as an SQL WHERE clause (excluding the WHERE itself). Passing null will return all rows for the given table.
String[] selectionArgs-----------在selection字段中可能会用'?'的形式来加一些额外的参数,这个的selectionArgs字段就是把selection字段的条件填充好(The values will be bound as Strings.). 如下的函数:
  public synchronized boolean mediaDirExists(String path) {
        Cursor cursor = mDb.query(DIR_TABLE_NAME,
                new String[] { DIR_ROW_PATH },
                DIR_ROW_PATH + "=?",
                new String[] { path },///<-----可能是多个填充,故使用数组
                null, null, null);
        boolean exists = cursor.moveToFirst();
        cursor.close();
        return exists;
    }
String groupBy  -----------一个过滤器,如何来分组---设置为null则不分组A filter declaring how to group rows, formatted as an SQL GROUP BY clause (excluding the GROUP BY itself). Passing null will cause the rows to not be grouped.?????
String having--------------分组后聚合的过滤条件A filter declare which row groups to include in the cursor, if row grouping is being used, formatted as an SQL HAVING clause (excluding the HAVING itself). Passing null will cause all row groups to be included, and is required when row grouping is not being used. ????????????
(groupBy和having不太懂.看下面的转载)

String orderBy  ------排序,格式是SQL的ORDER一样.设置null使用默认(无序unonder)排列.
 Cursor cursor = mDb.query(SEARCHHISTORY_TABLE_NAME,
                new String[] { SEARCHHISTORY_KEY },
                null, null, null, null,
                SEARCHHISTORY_DATE + " DESC",   ///<---DESC/ASC:降序/升序(格式是String orderBy = "_id desc")
                Integer.toString(size)); ///----
                
                
String limit   --------返回的行数,设置为null表示没有限制条款.

返回: A Cursor object, which is positioned before the first entry(第一个Entry). Note that Cursors are not synchronized, see the documentation for more details.(非同步的,故在函数外加public synchronized xxx(){});
////----------------------------------

  1. ///create this table
  2. String createSearchhistoryTabelQuery = "CREATE TABLE IF NOT EXISTS "
  3. + SEARCHHISTORY_TABLE_NAME + " ("
  4. + SEARCHHISTORY_KEY + " VARCHAR(200) PRIMARY KEY NOT NULL, "
  5. + SEARCHHISTORY_DATE + " DATETIME NOT NULL"
  6. + ");";
  7. db.execSQL(createSearchhistoryTabelQuery);
  8. public synchronized void addSearchhistoryItem(String key) {
  9. // set the format to sql date time
  10. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  11. Date date = new Date();
  12. ContentValues values = new ContentValues();
  13. values.put(SEARCHHISTORY_KEY, key);
  14. values.put(SEARCHHISTORY_DATE, dateFormat.format(date));
  15. mDb.replace(SEARCHHISTORY_TABLE_NAME, null, values);
  16. }
  17. public synchronized ArrayList<String> getSearchhistory(int size) {
  18. ArrayList<String> history = new ArrayList<String>();
  19. Cursor cursor = mDb.query(SEARCHHISTORY_TABLE_NAME,
  20. new String[] { SEARCHHISTORY_KEY },
  21. null, null, null, null,
  22. SEARCHHISTORY_DATE + " DESC",
  23. Integer.toString(size));
  24. while (cursor.moveToNext()) {
  25. history.add(cursor.getString(0));
  26. history.add(cursor.getString(1));
  27. }
  28. cursor.close();
  29. return history;
  30. }
  31. public synchronized void clearSearchhistory() {
  32. mDb.delete(SEARCHHISTORY_TABLE_NAME, null, null);
  33. }

----------------------------------------------------------------------
sql语句中GROUP BY 和 HAVING的使用 count()
在介绍GROUP BY 和 HAVING 子句前,我们必需先讲讲sql语言中一种特殊的函数:聚合函数, 
例如SUM, COUNT, MAX, AVG等。这些函数和其它函数的根本区别就是它们一般作用在多条记录上。

SELECT SUM(population) FROM bbc

这里的SUM作用在所有返回记录的population字段上,结果就是该查询只返回一个结果,即所有 
国家的总人口数。

having是分组(group by)后的筛选条件,分组后的数据组内再筛选
where则是在分组前筛选

通过使用GROUP BY 子句,可以让SUM 和 COUNT 这些函数对属于一组的数据起作用。 
当你指定 GROUP BY region 时, 属于同一个region(地区)的一组数据将只能返回一行值. 
也就是说,表中所有除region(地区)外的字段,只能通过 SUM, COUNT等聚合函数运算后返回一个值.

HAVING子句可以让我们筛选成组后的各组数据. 
WHERE子句在聚合前先筛选记录.也就是说作用在GROUP BY 子句和HAVING子句前. 
而 HAVING子句在聚合后对组记录进行筛选。

让我们还是通过具体的实例来理解GROUP BY 和 HAVING 子句,还采用第三节介绍的bbc表。

SQL实例:

一、显示每个地区的总人口数和总面积. 
SELECT region, SUM(population), SUM(area)
FROM bbc
GROUP BY region
 先以region把返回记录分成多个组,这就是GROUP BY的字面含义。分完组后,然后用聚合函数对每组中的不同字段(一或多条记录)作运算。

二、 显示每个地区的总人口数和总面积.仅显示那些面积超过1000000的地区。 
SELECT region, SUM(population), SUM(area)7 ]; Z& I! t% i
FROM bbc8 F4 w2 v( P- f
GROUP BY region
HAVING SUM(area)>1000000
在这里,我们不能用where来筛选超过1000000的地区,因为表中不存在这样一条记录。
相反,HAVING子句可以让我们筛选成组后的各组数据

android----sqlite中的 query() 参数分析的更多相关文章

  1. android sqlite中判断某个表是否存在

    <span style="font-size:18px;">sqlite 中判断某个表是否存在的方法,贴出来供大家参考 /** * 判断某张表是否存在 * @param ...

  2. android sqlite 中存储 long 数据

    在資料庫的技術中,一個資料庫(Database)表示應用程式儲存與管理資料的單位,應用程式可能需要儲存很多不同的資料,例如一個購物網站的資 料庫,就需要儲存與管理會員.商品和訂單資料.每一種在資料庫中 ...

  3. Android SQLite 通配符查询找不到参数问题

    使用Android SQLite中SQLiteDatabase类的query方法查询时,如果where中包含通配符,则参数会无法设置,如类似下面的方法查询时 SQLiteDatabase db = d ...

  4. Android开发中StackOverflowError

    Android开发中StackOverflowError错误实例分析 一.概述 我在一个复杂的layout嵌套较多的android界面,碰到了java.lang.StackOverflowError这 ...

  5. Android Sqlite 导入CSV文件 .

    http://blog.csdn.net/johnnycode/article/details/7413111 今天遇到 Oracle 导出的12万条CSV格式数据导入 Android Sqlite ...

  6. Android 开发中 SQLite 数据库的使用

    SQLite 介绍 SQLite 一个非常流行的嵌入式数据库,它支持 SQL 语言,并且只利用很少的内存就有很好的性能.此外它还是开源的,任何人都可以使用它.许多开源项目((Mozilla, PHP, ...

  7. SQLite在Android程序中的使用方法,SQLite的增删查改方法

    Sqlite: 1.一款用来实现本地数据存储的轻量级数据管理工具,是众多用来实现数据库管理的工具之一. 2.Android已经将SQLite的代码功能吸收在它的系统中,我们可以直接在Android程序 ...

  8. 在Android 开发中使用 SQLite 数据库笔记

    SQLite 介绍   SQLite 一个非常流行的嵌入式数据库,它支持 SQL 语言,并且只利用很少的内存就有很好的性能.此外它还是开源的,任何人都可以使用它.许多开源项目((Mozilla, PH ...

  9. Android SQLite性能分析

    作为Android预置的数据库模块,对SQLite的深入理解是很有必要的,能够从中找到一些优化的方向. 这里对SQLite的性能和内存进行了一些測试分析.对照了不同操作的运行性能和内存占用的情况,粗略 ...

随机推荐

  1. asp.net oracle 存储过程

    ORACLE代码 CREATE OR REPLACE PROCEDURE gd_CURSOR(MYCS1 OUT SYS_REFCURSOR,MYCS2 OUT SYS_REFCURSOR,a out ...

  2. [转]在WPF中使用WinForm控件方法

    本文转自:http://blog.csdn.net/lianchangshuai/article/details/6415241 下面以在Wpf中添加ZedGraph(用于创建任意数据的二维线型.条型 ...

  3. 51nod B君的圆锥(数学)

    题目链接: B君的圆锥 基准时间限制:1 秒 空间限制:131072 KB  B君要用一个表面积为S的圆锥将白山云包起来.   B君希望包住的白山云体积尽量大,B君想知道体积最大可以是多少.   注意 ...

  4. 【学习笔记】【C语言】sizeof

    1.用来计算一个变量或者一个常量.一种数据类型所占的内存字节数. 2.sizeof一共有3种形式 1>sizeof( 变量\常量 ) sizeof(10); char c = 'a'; size ...

  5. 【Unity3D】中的空引用 Null Reference Exception

    Null Reference Exception : Object reference not set to an instance of an object. 异常:空引用,对象的引用未设置到对象的 ...

  6. C# 数据操作工具类

    CREATE PROCEDURE [dbo].[RecordFromPage] @SelectList VARCHAR(max), @TableSource VARCHAR(100), @Search ...

  7. 利用ExcelDataReader封装类 导入表格数据

    nuget 添加Install-Package ExcelDataReader

  8. UI2_视图切换

    // // ViewController.m // UI2_视图切换 // // Created by zhangxueming on 15/7/1. // Copyright (c) 2015年 z ...

  9. 第一次使用easyUI

    一.项目结构图 二.在WebContent下新建resource文件夹,在resource底下创建easyui.将easyUI包放入其中. 三.在springMVC-servlet.xml写入资源路径 ...

  10. POJ 1273(EK)

    题目大概意思是,有N条水沟和M个水池,问从第一个水池到最后一个水池在同一时间内能够流过多少水第一行有两个整数N,M接下来N行,每行有3个整数,a,b,c,代表从a到b能够流c单位的水超级模板题,一个有 ...