Sqlite中如何返回唯一数据
发布时间:2020-12-12 19:42:09 所属栏目:百科 来源:网络整理
导读:SQLiteDatabase的Query方法都固定好参数了,distinct好像加不进去。 如果用execSQL方法在Select后面带上distinct,返回结果为void,得不到Cursor。 请问在Sqlite中怎样才能去掉重复得到唯一数据。 方案: SQLiteDatabase类另有一个rawQuery方法,可以用自定
SQLiteDatabase的Query方法都固定好参数了,distinct好像加不进去。
如果用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,153); text-decoration: none;">SQLiteDatabase</a>db=....; <a target=_blank href="http://www.iteedu.com/handset/android/sqlitediary/Cursor.php" style="color: rgb(51,153); text-decoration: none;">Cursor</a>cursor=db.rawQuery("select*fromperson",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,153); text-decoration: none;">Cursor</a>cursor=db.rawQuery("select*frompersonwherename like?andage=?",newString[]{"%iteedu%","4"});
对于Android平台上的数据库而言使用了嵌入式越来越流行的SQLite,为了更好的跨平台我们推荐大家使用原始SQL语句直接操作,在代码和处理效率上都有不小的提高,不过要做好SQL语句异常处理。
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |