• Android中SQLite数据库查询详解


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

    SQLite中查询方法query()

    query(String table, String[] columns, String selection, String [] selectionArgs, String  groupBy,  String having, String orderBy, String  limit)
    
    • 1
    • 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<PersonModel> queryAllPersonData(){
    
            //查询全部数据
            Cursor cursor = getWritableDatabase().query(TABLE_NAME_PERSON,null,null,null,null,null,null,null);
            List<PersonModel> 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;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    一些查询用法示例

        /**
         * 一些查询用法
         */
        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");
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    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();
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 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 = 1
            rawQuerySql = "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 desc
            rawQuerySql = "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, 4
            rawQuerySql = "select * from "+TABLE_NAME_PERSON+" order by "+VALUE_ID +" desc"+" limit 0, 4";
    
            //查询年龄在20岁以上或者是女生 的数据 select age,isboy from person where age > 20 or isboy != 1
            rawQuerySql = "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 asc
            rawQuerySql = "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;
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    Android SQLite的增删改查详解链接

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

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

  • 相关阅读:
    Spark 结构化流写入Hudi 实践
    android 自定义View:仿QQ拖拽效果
    Windows下文本生成图像AI画图尝鲜体验
    前端JS基础第三篇:七道例题带你弄懂this指向问题
    SpringBoot 集成 Netty
    博客系统(页面设计)
    操作系统学习
    Vue 插槽(slot)使用
    30 个Python代码实现的常用功能(附案例源码)
    测试软件要求规范 (SRS)
  • 原文地址:https://blog.csdn.net/mfysss/article/details/128059700