android 中 sqlite sql語句 參數 分析

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章