您的位置:首页 > 产品设计 > UI/UE

android------sqlite中的 query() 参数分析-----------------------------------

2016-04-25 15:09 435 查看
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
plain copy

///create this table  

  String createSearchhistoryTabelQuery = "CREATE TABLE IF NOT EXISTS "  

                    + SEARCHHISTORY_TABLE_NAME + " ("  

                    + SEARCHHISTORY_KEY + " VARCHAR(200) PRIMARY KEY NOT NULL, "  

                    + SEARCHHISTORY_DATE + " DATETIME NOT NULL"  

                    + ");";  

  

  

            db.execSQL(createSearchhistoryTabelQuery);  

              

 public synchronized void addSearchhistoryItem(String key) {  

        // set the format to sql date time  

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  

        Date date = new Date();  

        ContentValues values = new ContentValues();  

        values.put(SEARCHHISTORY_KEY, key);  

        values.put(SEARCHHISTORY_DATE, dateFormat.format(date));  

  

  

        mDb.replace(SEARCHHISTORY_TABLE_NAME, null, values);  

    }  

    public synchronized ArrayList<String> getSearchhistory(int size) {  

        ArrayList<String> history = new ArrayList<String>();  

  

  

      Cursor cursor = mDb.query(SEARCHHISTORY_TABLE_NAME,  

                new String[] { SEARCHHISTORY_KEY },  

                null, null, null, null,  

                SEARCHHISTORY_DATE + " DESC",  

                Integer.toString(size));  

        while (cursor.moveToNext()) {  

            history.add(cursor.getString(0));  

            history.add(cursor.getString(1));  

        }  

        cursor.close();  

  

  

        return history;  

    }  

    public synchronized void clearSearchhistory() {  

        mDb.delete(SEARCHHISTORY_TABLE_NAME, null, null);  

    }  

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

一下是具体的实例,大家注意看下:

SQLiteDatabase 给我提供的方法很不实用,还是建议楼主自己写sql语句,参数想怎么传都可以
例如:Cursor c = db.rawQuery("select * from user where username=? and password = ?",
new Stirng[]{"用户名","密码"});

如果你非要调用SQLiteDatabase的query方法,那可以这样
db.query("表名", new String[]{"字段1,字段2"}, "条件1=? and 条件2=?", new String[]{"条件1的值,条件2的值"},null,null,null)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: