android----sqlite中的 query() 參數分析

來源:互聯網
上載者:User

標籤:

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.                 null, null, null, null,  
  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, null, null);  
  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子句可以讓我們篩選成組後的各組資料

android----sqlite中的 query() 參數分析

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.