首先咱們假設咱們有以下表格,表格名稱「Employees」: java
Id |
LastName |
FirstName |
Address |
City |
1 |
Adams |
John |
Oxford Street |
London |
2 |
Bush |
George |
Fifth Avenue |
New York |
3 |
Carter |
Thomas |
Changan Street |
Beijing |
SQL基本格式以下: sql
最基本SQL語句,就是從表中選取要返回的列數據,不加任何過濾條件。固然若是咱們的「列名稱」是"*",那麼將返回整個表格數據。在 Android上,SQL相關的method一般有一個參數就是String[] columns,而這個參數對應的就是SQL語句中的「列名稱」。咱們能夠看一個Android中的method - query: 函數
- public Cursor query (String table, String[] columns, String selection, String[] selectionArgs,
- String groupBy, String having, String orderBy, String limit)
- public Cursor query (String table, String[] columns, String selection, String[] selectionArgs,
- String groupBy, String having, String orderBy, String limit)
假設咱們想獲取人員的全名,那麼SQL語句以下: spa
- SELECT FirstName, LastName FROM Employees
- SELECT FirstName, LastName FROM Employees
固然通常地咱們會有條件的過濾咱們想要的結果,例如我只想返回city爲Beijing的人員信息,那麼我就須要用WHERE來過濾: orm
- SELECT * FROM Employees WHERE City= 'Beijing'
- SELECT * FROM Employees WHERE City='Beijing'
這裏where後面跟的字符串就是Android的method中對應得參數String selection。Android的method中一般還有個參數與此相關就是 String[] selectionArgs,當 selection參數中包含問號"?"時,那麼selectionArgs纔會用到。舉個例子假設selection參數賦值以下: 排序
- String selection = 」City=?";
- String selection = 」City=?";
這時咱們必須在selectionArgs中賦值 ci
- String[] selectionArgs = { "Beijing" };
- String[] selectionArgs = {"Beijing"};
也就是說selectionArgs中的字符串就是對應selection中的問號所表明的變量。實際上就是讓selection中的過濾條件City能夠動態的賦值,而不是寫死在程序當中。在 query() 執行時會對 selectionArgs 中的字符串正確轉義並替換到對應的 ? 處以構成完整的 selection 字符串。 有點像 String.format()。 字符串
那麼很顯然參數String groupBy就是對應SQL語句中GROUP BY後面的字符串,GROUP BY是與合計函數(Aggregate Functions)如SUM()一塊兒使用的。詳細的用法你們能夠從網上查查。 it
參數String having對應SQL語句HAVING後面的字符串,也是要與合計函數一塊兒使用的。 io
參數String orderBy對應SQL語句ORDER BY後面的字符串。
參數limit指明返回的rows的數量。
咱們下面舉個例子,假設有以下數據表,表名"Orders":
Id |
CustomerName |
OrderPrice |
Country |
OrderDate |
1 |
Arc |
100 |
China |
2010/1/2 |
2 |
Bor |
200 |
USA |
2010/3/20 |
3 |
Cut |
500 |
Japan |
2010/2/20 |
4 |
Bor |
300 |
USA |
2010/3/2 |
5 |
Arc |
600 |
China |
2010/3/25 |
6 |
Doom |
200 |
China |
2010/3/26 |
假設咱們想查詢客戶總的訂單數在500元以上的,且County在中國的客戶的名稱和訂單總數,且按照CustomerName來排序,默認ASC排序,那麼SQL語句應當是:
- SELECT CustomerName, SUM (OrderPrice) FROM Orders WHERE Country=?
- GROUP BY CustomerName
- HAVING SUM (OrderPrice)>500
- ORDER BY CustomerName
- SELECT CustomerName, SUM(OrderPrice) FROM Orders WHERE Country=?
- GROUP BY CustomerName
- HAVING SUM(OrderPrice)>500
- ORDER BY CustomerName
那麼對應Android的query函數的參數以下:
- String table = "Orders" ;
- String[] columns = new String[] { "CustomerName" , "SUM(OrderPrice)" };
- String selection = "Country=?" ;
- String[] selectionArgs = new String[]{ "China" };
- String groupBy = "CustomerName" ;
- String having = "SUM(OrderPrice)>500" ;
- String orderBy = "CustomerName" ;
- Cursor c = db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy, null);
- String table = "Orders";
- String[] columns = new String[] {"CustomerName", "SUM(OrderPrice)"};
- String selection = "Country=?";
- String[] selectionArgs = new String[]{"China"};
- String groupBy = "CustomerName";
- String having = "SUM(OrderPrice)>500";
- String orderBy = "CustomerName";
- Cursor c = db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy, null);
查詢的結果應該是:
CustomerName |
SUM(OrderPrice) |
Arc |
700 |