android----sqlite中的 query() 参数分析
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(){});
////----------------------------------
- ///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子句可以让我们筛选成组后的各组数据
android----sqlite中的 query() 参数分析的更多相关文章
- android sqlite中判断某个表是否存在
<span style="font-size:18px;">sqlite 中判断某个表是否存在的方法,贴出来供大家参考 /** * 判断某张表是否存在 * @param ...
- android sqlite 中存储 long 数据
在資料庫的技術中,一個資料庫(Database)表示應用程式儲存與管理資料的單位,應用程式可能需要儲存很多不同的資料,例如一個購物網站的資 料庫,就需要儲存與管理會員.商品和訂單資料.每一種在資料庫中 ...
- Android SQLite 通配符查询找不到参数问题
使用Android SQLite中SQLiteDatabase类的query方法查询时,如果where中包含通配符,则参数会无法设置,如类似下面的方法查询时 SQLiteDatabase db = d ...
- Android开发中StackOverflowError
Android开发中StackOverflowError错误实例分析 一.概述 我在一个复杂的layout嵌套较多的android界面,碰到了java.lang.StackOverflowError这 ...
- Android Sqlite 导入CSV文件 .
http://blog.csdn.net/johnnycode/article/details/7413111 今天遇到 Oracle 导出的12万条CSV格式数据导入 Android Sqlite ...
- Android 开发中 SQLite 数据库的使用
SQLite 介绍 SQLite 一个非常流行的嵌入式数据库,它支持 SQL 语言,并且只利用很少的内存就有很好的性能.此外它还是开源的,任何人都可以使用它.许多开源项目((Mozilla, PHP, ...
- SQLite在Android程序中的使用方法,SQLite的增删查改方法
Sqlite: 1.一款用来实现本地数据存储的轻量级数据管理工具,是众多用来实现数据库管理的工具之一. 2.Android已经将SQLite的代码功能吸收在它的系统中,我们可以直接在Android程序 ...
- 在Android 开发中使用 SQLite 数据库笔记
SQLite 介绍 SQLite 一个非常流行的嵌入式数据库,它支持 SQL 语言,并且只利用很少的内存就有很好的性能.此外它还是开源的,任何人都可以使用它.许多开源项目((Mozilla, PH ...
- Android SQLite性能分析
作为Android预置的数据库模块,对SQLite的深入理解是很有必要的,能够从中找到一些优化的方向. 这里对SQLite的性能和内存进行了一些測试分析.对照了不同操作的运行性能和内存占用的情况,粗略 ...
随机推荐
- HDU 2084 数塔 (DP)
数塔 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Pr ...
- 浅谈iOS的Autolayout
iOS 应用,通常是以界面(UI: User Interface)展示给用户. 没有界面的应用,对用户来说,将无所适从,总不至于罗列出一堆说明书吧. iOS开发,很大一部分工作量都花在界面布局上,有时 ...
- UIActionSheet警告,提示调用showFromTabBar方法
UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"选择图片" delegate:(self) c ...
- WGS84坐标系下,经纬度如何换算成米
参考博客:显示瓦片地图 http://www.cnblogs.com/rhinoxy/p/4995731.html 注意:这里的计算方法精度相差比较大,不满足精确计算的需要. 需要理解的GIS概念: ...
- ASP.Net 验证控件 CompareValidator
CompareValidator 作用就是比较两个输入框所输入的值是否一致,常用来作为验证输入密码和确认密码的业务. 属性 描述 Operator 比较操作符,可选值DataTypeCheck.Equ ...
- MongoDB - MongoDB CRUD Operations
CRUD operations create, read, update, and delete documents. Create Operations Create or insert opera ...
- 每天一道LeetCode--389. Find the Difference
Given two strings s and t which consist of only lowercase letters. String t is generated by random s ...
- Android ListView动态改变Item高度
在adapter的getView方法中进行设置,代码如下 @Override public View getView(int position, View convertView, ViewGroup ...
- OS中常用的调度算法总结 (转)
http://blog.chinaunix.net/uid-25132162-id-361291.html 一.常见的批处理作业调度算法 1.先来先服务调度算法(FCFS):就是按照各个作业进入系统的 ...
- js 日期函数用法总结
1 创建Date对象,用于处理日期和时间 var date=new Date(); Date对象会把当前日期和时间保存为初始值. 还可以设置其它参数初始化 Date对象: new Date(" ...