Android中SQLite数据库查询详解
admin
2024-02-18 11:32:08
0次

SQLite在线教程网址https://www.yiibai.com/sqlite

文章目录

  • SQLite中查询方法query()
    • 查询全部数据示例
    • 一些查询用法示例
  • rawQuery查询
    • select语句查询
  • Android SQLite的增删改查详解链接

SQLite中查询方法query()

query(String table, String[] columns, String selection, String [] selectionArgs, String  groupBy,  String having, String orderBy, String  limit)
  • table :表名。相当于select *** from table语句中的table 。如果是多表联合查询,可以用逗号将两个表名分开。
  • columns :要查询出来的列名(字段),全部查询就写null。相当于 select *** from table语句中的 ***部分 。如果是查询多个参数,可以用逗号将两个表名分开。例:new String[]{“name”,“age”,“sex”}
  • selection:查询条件子句,相当于select *** from table where && 语句中的&&部分,在条件子句允许使用占位符“?”表示条件值 ,例:“name=?,age=?,sex=?”
  • selectionArgs :查询条件对应的内容。对应于 selection参数 占位符的值,值在数组中的位置与占位符在语句中的位置必须一致,否则就会有异常。 例:与 new String[]{“lisa”,“1”,“女”}
  • groupBy :分组。相当于 select *** from table where && group by … 语句中 … 的部分 ,作用是:将同一列的相同名字的参数合并在一起 例;在name列有两个Jame(name:Jame --salay:100,name:Jame --salay:200),使用…group by name查询后 只显示一个Jame的集合(name:Jame–salay:300)
  • having :相当于 select *** from table where && group by …having %%% 语句中 %%% 的部分, 作用于groupBy的条件,例:havig name>2意思是name列相同参数>2
  • orderBy :相当于 select ***from ?? where&& group by …having %%% order by@@语句中的@@ 部分,如: personid desc(按person 降序), age asc(按age升序);
  • limit:指定偏移量和获取的记录数,查询显示的条数(分页,可以不写,默认全部显示)。相当于select语句limit关键字后面的部分。

查询全部数据示例

    /*** 查询全部数据*/public List queryAllPersonData(){//查询全部数据Cursor cursor = getWritableDatabase().query(TABLE_NAME_PERSON,null,null,null,null,null,null,null);List list = new ArrayList<>();if(cursor.getCount() > 0){//移动到首位cursor.moveToFirst();for (int i = 0; i < cursor.getCount(); i++) {int id = cursor.getInt(cursor.getColumnIndex(VALUE_ID));String name = cursor.getString(cursor.getColumnIndex(VALUE_NAME));int isBoy = cursor.getInt(cursor.getColumnIndex(VALUE_ISBOY));int age = cursor.getInt(cursor.getColumnIndex(VALUE_AGE));String address = cursor.getString(cursor.getColumnIndex(VALUE_ADDRESS));byte pic[] = cursor.getBlob(cursor.getColumnIndex(VALUE_PIC));PersonModel model = new PersonModel();model.setId(id);model.setName(name);model.setIsBoy(isBoy);model.setAge(age);model.setAddress(address);model.setPic(pic);list.add(model);//移动到下一位cursor.moveToNext();}}cursor.close();getWritableDatabase().close();return list;}

一些查询用法示例

    /*** 一些查询用法*/public void queryPersonData(){//查询全部数据getWritableDatabase().query(TABLE_NAME_PERSON,null,null,null,null,null,null);//查询 _id = 1 的数据getWritableDatabase().query(TABLE_NAME_PERSON,null,VALUE_ID+"=?",new String[]{"1"},null,null,null);//查询 name = 张三 并且 age > 23 的数据getWritableDatabase().query(TABLE_NAME_PERSON,null,VALUE_NAME+"=?"+" and "+VALUE_AGE+">?",new String[]{"张三","23"},null,null,null);//查询 name = 张三 并且 age > 23 的数据  并按照id 降序排列getWritableDatabase().query(TABLE_NAME_PERSON,null,VALUE_NAME+"=?"+" and "+VALUE_AGE+">?",new String[]{"张三","23"},null,null,VALUE_ID+" desc");//查询数据按_id降序排列 并且只取前4条。getWritableDatabase().query(TABLE_NAME_PERSON,null,null,null,null,null,VALUE_ID+" desc","0,4");}

rawQuery查询

rawQuery()是自己要写sql语句查询,query()也是在源码里合成sql语句的。所以自己写sql语句更加高效。
public Cursor rawQuery(String sql, String[] selectionArgs)

rawQuery方法至少有两个参数。使用:

SQLiteDatabase db= ....;
Cursor cursor = db.rawQuery("select * from person",null);
...
cursor.close();
db.close();
  • rawQuery()方法的第一个参数为select语句;
  • 第二个参数为select语句中占位符参数的值,如果select语句没有使用占位符,该参数可以设置为null
    带占位符参数的select语句使用例子如下:
    Cursor cursor = db.rawQuery("select * from person where name like ? and age=?", new String[]{"张三", "4"});

select语句查询

  /*** rawQuery()方法查询** 一些查询用法** 容易出错,万千注意。** 注意空格、单引号、单词不要写错了。**/public Cursor rawQueryPersonData() {Cursor cursor = null;String rawQuerySql = null;//查询全部数据  select * from person rawQuerySql =  "select * from "+TABLE_NAME_PERSON;//查询_id = 1 的数据  select * from person where _id = 1rawQuerySql = "select * from "+TABLE_NAME_PERSON+" where "+VALUE_ID +" = 1";//查询 name = 张三 并且 age > 23 的数据  通配符? select * from person where name = ? and age > ?rawQuerySql = "select * from "+TABLE_NAME_PERSON+" where "+VALUE_NAME +" = ?"+" and "+ VALUE_AGE +" > ?";
//        cursor = getWritableDatabase().rawQuery(rawQuerySql,new String[]{"张三","23"});//查询 name = 张三 并且 age >= 23 的数据  select * from person where name = '张三' and age >= '23'rawQuerySql = "select * from "+TABLE_NAME_PERSON+" where "+VALUE_NAME +" = '张三'"+" and "+ VALUE_AGE +" >= '23'";//查询 name = 张三 并且 age >= 23 的数据  并按照id 降序排列  select * from person where name = '张三' and age >= '23' order by _id descrawQuerySql = "select * from "+TABLE_NAME_PERSON+" where "+VALUE_NAME +" = '张三'"+" and "+ VALUE_AGE +" >= '23'"+" order by "+VALUE_ID +" desc";//查询数据按_id降序排列 并且只取前4条。(测试下标是从0开始)  select * from person order by _id desc limit 0, 4rawQuerySql = "select * from "+TABLE_NAME_PERSON+" order by "+VALUE_ID +" desc"+" limit 0, 4";//查询年龄在20岁以上或者是女生 的数据 select age,isboy from person where age > 20 or isboy != 1rawQuerySql = "select "+VALUE_AGE+","+VALUE_ISBOY +" from " +TABLE_NAME_PERSON+" where "+VALUE_AGE+" > 20"+" or "+VALUE_ISBOY +" != 1";//查询年龄小于等于20 或者 大于等于 80的数据 并且按年龄升序排列 select * from person where age <= 20 or age >=80 order by age ascrawQuerySql = "select * from "+TABLE_NAME_PERSON+" where "+VALUE_AGE+" <= 20"+" or "+VALUE_AGE+" >=80"+" order by "+VALUE_AGE+" asc";cursor = getWritableDatabase().rawQuery(rawQuerySql,null);Log.e(TAG, rawQuerySql );return cursor;}

Android SQLite的增删改查详解链接

SQLite数据库,创建表
SQLite数据库,添加数据
SQLite数据库 删除数据 where多条件删除数据
SQLite数据库 修改数据
SQLite数据库 更新

本文参考:
http://blog.csdn.net/kongxiuqi/article/details/50528005
https://www.jianshu.com/p/4f67e8c3463b
---------感谢前辈

相关内容

热门资讯

AI做攻略、“双向奔赴”……国... 本文转自:人民网人民网记者 许维娜今年国庆假期,AI(人工智能)成了不少年轻人的“旅行搭子”。行前做...
合肥的这个“梗”, 翻篇了 社交平台的评论区,曾长期流传一句专属于合肥的黑色幽默。有人问合肥哪里好玩,本地网友半开玩笑地回复:合...
7万+旅游中高层都在的群,今天... 做旅游这行,资源卡位、渠道互通、项目落地——往往就差一个“对的机会”。我们的行业社群已聚集70,00...
一场帐篷音乐节,如何让井研从“... 帐篷音乐节现场封面新闻记者 周洪攀10月的研溪湿地,晚风里裹着柑橘的清甜与民谣的回响。夜幕降临,10...
白鹤翩翩落星愿,上海迪士尼度假... 10月9日,世界候鸟日前夕,上海迪士尼度假区星愿公园的湖风里多了一分白鹤的灵动。三座白鹤雕塑正式揭幕...
成都锦江边赏初秋枫叶飘飘 目前,成都锦江边国华街枫叶树红了。近年来,国华街附近的几条街都栽种了红枫树,是城区较集中看枫叶的点位...
GBTA:商务旅行正在复苏,但... 亚太正在成为全球商务旅行增长的重要引擎。在经历疫情后的快速反弹之后,全球商务旅行正在进入一个新的增长...
从景区焕新到节赛引流 邹城双节... 齐鲁网·闪电新闻10月9日讯 中秋、国庆“双节”假期,邹城市文化和旅游局提前谋划、周密部署,立足自身...
江山如此多娇 且看石楼今朝 金秋十月,黄河岸畔秋意盎然,丰收盛会喜迎国庆。本次石楼国庆文旅活动以“江山如此多娇,且看石楼今朝”为...
海南国庆假期接待游客475.4... 2026年国庆假期,海南接待游客475.40万人次,较2025年增长13.1%,较2024年增长15...