android 中 sqlite sql语句 参数 分析

来源:互联网 发布:学会python要多久 编辑:程序博客网 时间:2024/05/01 14:42
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(){});
////----------------------------------
[java] view plaincopy
  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.   
  8.   
  9.             db.execSQL(createSearchhistoryTabelQuery);  
  10.               
  11.  public synchronized void addSearchhistoryItem(String key) {  
  12.         // set the format to sql date time  
  13.         SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  14.         Date date = new Date();  
  15.         ContentValues values = new ContentValues();  
  16.         values.put(SEARCHHISTORY_KEY, key);  
  17.         values.put(SEARCHHISTORY_DATE, dateFormat.format(date));  
  18.   
  19.   
  20.         mDb.replace(SEARCHHISTORY_TABLE_NAME, null, values);  
  21.     }  
  22.     public synchronized ArrayList<String> getSearchhistory(int size) {  
  23.         ArrayList<String> history = new ArrayList<String>();  
  24.   
  25.   
  26.       Cursor cursor = mDb.query(SEARCHHISTORY_TABLE_NAME,  
  27.                 new String[] { SEARCHHISTORY_KEY },  
  28.                 nullnullnullnull,  
  29.                 SEARCHHISTORY_DATE + " DESC",  
  30.                 Integer.toString(size));  
  31.         while (cursor.moveToNext()) {  
  32.             history.add(cursor.getString(0));  
  33.             history.add(cursor.getString(1));  
  34.         }  
  35.         cursor.close();  
  36.   
  37.   
  38.         return history;  
  39.     }  
  40.     public synchronized void clearSearchhistory() {  
  41.         mDb.delete(SEARCHHISTORY_TABLE_NAME, nullnull);  
  42.     }  





----------------------------------------------------------------------
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子句可以让我们筛选成组后的各组数据

转载原地址:http://blog.csdn.net/sno_guo/article/details/9323987

0 0
原创粉丝点击