Sqlite中如何返回唯一数据
来源:互联网 发布:135端口入侵 编辑:程序博客网 时间:2024/05/29 21:31
SQLiteDatabase的Query方法都固定好参数了,distinct好像加不进去。
如果用execSQL方法在Select后面带上distinct,返回结果为void,得不到Cursor。
如果用execSQL方法在Select后面带上distinct,返回结果为void,得不到Cursor。
请问在Sqlite中怎样才能去掉重复得到唯一数据。
方案:
SQLiteDatabase类另有一个rawQuery方法,可以用自定义的SQL语句进行查询,返回Cursor类型。用这个方法可以进行稍微复杂一点的查询了。
SQLiteDatabase的rawQuery()用于执行select语句,使用例子如下:
<a target=_blank href="http://www.iteedu.com/handset/android/sqlitediary/SQLiteDatabase.php" style="color: rgb(51, 102, 153); text-decoration: none;">SQLiteDatabase</a> db= ....;<a target=_blank href="http://www.iteedu.com/handset/android/sqlitediary/Cursor.php" style="color: rgb(51, 102, 153); text-decoration: none;">Cursor</a> cursor = db.rawQuery("select * from person",null);...cursor.close();db.close();rawQuery()方法的第一个参数为select语句;第二个参数为select语句中占位符参数的值,如果select语句没有使用占位符,该参数可以设置为null。带占位符参数的select语句使用例子如下:
<a target=_blank href="http://www.iteedu.com/handset/android/sqlitediary/Cursor.php" style="color: rgb(51, 102, 153); text-decoration: none;">Cursor</a> cursor = db.rawQuery("select * from personwhere name like ?and age=?", new String[]{"%iteedu%", "4"});
对于Android平台上的数据库而言使用了嵌入式越来越流行的SQLite,为了更好的跨平台我们推荐大家使用原始SQL语句直接操作,在代码和处理效率上都有不小的提高,不过要做好SQL语句异常处理。
下面我们说下rawQuery的好处,可以看到查询的代码直接使用SQL语句,通过性能实测效率比Android封装过的类要快不少,但不能配合一些 Adapter的使用,不过总体上在跨平台上很突出,下面为本地使用方法的伪代码,没有做任何构造和实例化,希望让项目经理知道rawSQL的优势在 Android平台上的使用。
- SQLiteDatabase db;
- String args[] = {id};
- ContentValues cv = new ContentValues();
- cv.put("android123", id);
- Cursor c = db.rawQuery("SELECT * FROM table WHERE android123=?", args); 执行本地SQL语句查询
- if (c.getCount() != 0) {
- //dosomething
- ContentValues cv = new ContentValues();
-
- cv.put("android123","cwj");
- db.insert("table", "android123", cv); //插入数据
- String args[] = {id};
- ContentValues cv2= new ContentValues();
-
- cv2.put("android123", id);
- db.delete("table", "android123=?", args); //删除数据
- }
0 0
- Sqlite中如何返回唯一数据
- android中Sqlite查询的数据返回排序问题
- 如何保证Excel中数据的唯一性
- php中如何读取sqlite数据库文件的数据
- 如何提升sqlite中blob数据的查询性能
- 如何在MySQL的SQL语句中避免数据唯一性冲突
- delphi 中如何调用webservice返回dataset 数据集
- js中如何处理服务端返回的数据集
- restlet中如何返回json和xml类型数据
- android中如何在得到返回数据后设置图片
- Spring MVC中Controller如何将数据返回给页面
- 使用Mybatis插入数据时返回唯一主键值
- EXCEL数据导入Sqlite中
- Android中SQLite数据操作
- sqlite中插入数据无效
- JMeter中如何构造一个唯一ID
- SQLite中如何连接字符串
- Android中如何使用SQLite
- Codeforces Round #289 (Div. 2, ACM ICPC Rules) D. Restoring Numbers 构造 数学
- 学习STL过程中的一些零散记录4
- EularProject 34: 一个数字与他每位数的阶乘和
- 十六进制色码表
- roboguice2教程
- Sqlite中如何返回唯一数据
- centos下nginx+tomcat安装配置 nginx结合tomcat
- LeetCode Remove Linked List Elements
- Spring AOP + Redis缓存数据库查询
- 使用Objective-C的文档生成工具:appledoc
- git
- Hibernate初入门4--hbm2ddl
- LeetCode Count Primes
- POJ2406 & POJ1961 详细解题思路和AC程序 KMP算法变种