SQLite用google封装的API进行增删改查

主要是参照官方文档SQLiteDatabase的方法 详细信息见API详情


SQLite用google封装的API进行增删改查_第1张图片
image.png
    /**
     * 增加一条数据
     * @param view
     */
    public void add(View view) {
        SQLiteDatabase db = helper.getWritableDatabase();
        String sql = "insert into info(name,phone) values(?,?)";
        String name = "mengk2";
        String phone = "17777777772";
//      db.execSQL(sql,new Object[]{name,phone});
        
        //ContentValues 内部封装了一个map   key:  对应列的名字  value对应的值
        String table = "info";
        ContentValues values = new ContentValues();
        values.put("name", name);
        values.put("phone", phone);
        //底层就在组拼sql语句  //返回值代表插入新行的id 
        long insert = db.insert(table, null, values);
        if (insert > 0) {
            Toast.makeText(getApplicationContext(), "插入成功", 0);
        } else {
            Toast.makeText(getApplicationContext(), "插入失败", 0);
        }
        
        db.close();
    }
    
    /**
     * 删除数据
     * @param view
     */
    public void delete(View view) {
        SQLiteDatabase db = helper.getWritableDatabase();
//      db.execSQL("delete from info where name=?", new Object[]{"mengk"});
        
        //返回值代表影响的行数 
        int delete = db.delete("info", "name=?", new String[]{"mengk2"});
        Toast.makeText(getApplicationContext(), "删除了" + delete + "行", 0);
        db.close();
    }
    
    /**
     * 更新数据
     * @param view
     */
    public void update(View view) {
        String sql = "update info set phone=? where name=?";
        String name = "mengk";
        String phone = "18888888888";
        SQLiteDatabase db = helper.getWritableDatabase();
        
//      db.execSQL(sql, new Object[]{phone,name});
        
        ContentValues values = new ContentValues();
        values.put("name", "mengkmu");
        values.put("phone", "m1838888888");
        String whereClause = "name=?";
        String[] whereArgs = new String[]{"mengk2"};
        //更新了多少行
        int update = db.update("info", values, whereClause, whereArgs);
        Toast.makeText(getApplicationContext(), "更新了"+update+"行", 0).show();
        
        db.close();
        
    }
    
    /**
     * 查询数据
     * @param view
     */
    public void query(View view) {
        String sql = "select * from info";
        SQLiteDatabase db = helper.getWritableDatabase();
//      Cursor cursor = db.rawQuery(sql, null);
        
        //columns 代表你要查询的列
        //selection 根据什么查询phone 
//      Cursor cursor = db.query("info", new String[]{"name","phone"}, "name=?", new String[]{"mengk2"}, null, null, null);
        Cursor cursor = db.query("info", null,null, null, null, null, null);
        
        if (cursor != null && cursor.getColumnCount() > 0) {
            while (cursor.moveToNext()) {
                String name = cursor.getString(1);
                String phone = cursor.getString(2);
                System.out.println("name:"+name+"----"+phone);
            }
            cursor.close();
        }
        db.close();
        
    }

优点:不需要写sql
有返回值便于开发

缺点:如果有多张表 API不容易查询

你可能感兴趣的:(SQLite用google封装的API进行增删改查)