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的性能和内存进行了一些測试分析.对照了不同操作的运行性能和内存占用的情况,粗略 ...
随机推荐
- VMware系统运维(十六)部署虚拟化桌面 Horizon View Manager 5.2 配置池
1.点击"添加",打开添加池界面,选择"自动池",点击"下一步" 2.选择"专用,启动自动分配",点击"下一步 ...
- Win10环境下使用VS2015编译PJProject
一.pjproject源码下载 下载地址:http://www.pjsip.org/ 二.编译之前 1.pj配置,通常直接复制并重命名config_site_simple.h为config_site. ...
- ServiceLoader的使用
ServiceLoader与ClassLoader是Java中2个即相互区别又相互联系的加载器.JVM利用ClassLoader将类载入内存,这是一个类声明周期的第一步(一个java类的完整的生命周期 ...
- codeforces 590C C. Three States(bfs+连通块之间的最短距离)
题目链接: C. Three States time limit per test 5 seconds memory limit per test 512 megabytes input standa ...
- 浅谈iOS的Autolayout
iOS 应用,通常是以界面(UI: User Interface)展示给用户. 没有界面的应用,对用户来说,将无所适从,总不至于罗列出一堆说明书吧. iOS开发,很大一部分工作量都花在界面布局上,有时 ...
- 用jquery 实现 超出字符 截断加上省略号并且可以提示全部内容
1.test.jsp Java代码 <%@ page language="java" pageEncoding="UTF-8"%> <html ...
- html5 requestAnimationFrame制作动画:旋转风车
详细内容请点击 在以往,我们在网页上制作动画效果的时候,如果是用javascript实现,一般都是通过定时器和间隔来实现的,出现HTML5之后,我们还可以用CSS3 的transitions和anim ...
- CSS其他
1.元素的宽度由内容撑开 display:inline;——不支持高度 display:inline-block;——在IE6下,不支持块标签 float position:absolute——每项设 ...
- 手把手教你认识并搭建Nginx
手把手教你认识并搭建Nginx Nginx (“engine x”) 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器. Nginx 是由 Igor ...
- 【Linux C中文函数手册】之 内存和字符串函数
内存和字符串函数 1) bcmp 比较内存内容 相关函数 bcmp,strcasecmp,strcmp,strcoll,strncmp,strncasecmp表头文件 #include<stri ...