在上一章GreenDao3.0学习(二)中,我们知道了怎么使用greendao创建数据,这一章主要是介绍greendao的增删改查

增加

代码如下:

   private void insertUser(Long id, String name) {
        UserBeanDao userDao = GreenDaoManager.getInstance().getSession().getUserBeanDao();
        UserBean user = new UserBean(id, name);
        userDao.insert(user);
        mNameET.setText("");
        mUserList.clear();
        mUserList.addAll(userDao.queryBuilder().build().list());
        mUserAdapter.notifyDataSetChanged();
    }

没错,就是这么简单:
这里需要注意的是:insertUser中的id我们只要传null就行,比如insertUser(null, str);
看看效果:
增加.gif
插入成功:

删除

  private void deleteUser(String name) {
        UserBeanDao userBeanDao = GreenDaoManager.getInstance().getSession().getUserBeanDao();
        //删除
        UserBean findUser = userBeanDao.queryBuilder().where(UserBeanDao.Properties.Name.eq(name)).build().unique();
        if (findUser != null) {
            userBeanDao.deleteByKey(findUser.getId());
        }
//        //根据某一个条件批量删除
//        List<UserBean> userList = userBeanDao.queryBuilder().where(UserBeanDao.Properties.Name.eq(name)).build().list();
//        for (UserBean user : userList) {
//            userBeanDao.delete(user);
//        }

        mNameET.setText("");
        mUserList.clear();
        mUserList.addAll(userBeanDao.queryBuilder().build().list());
        mUserAdapter.notifyDataSetChanged();
    }

根据name删除单个:
单个删除.gif
根据name批量删除:

批量删除.gif

修改/更新

  private void updateUser(String prevName, String newName) {
        UserBeanDao userBeanDao = GreenDaoManager.getInstance().getSession().getUserBeanDao();
        //更新单个
        UserBean findUser = GreenDaoManager.getInstance().getSession().getUserBeanDao().queryBuilder()
                .where(UserBeanDao.Properties.Name.eq(prevName)).build().unique();
        if (findUser != null) {
            findUser.setName(newName);
            GreenDaoManager.getInstance().getSession().getUserBeanDao().update(findUser);
            Toast.makeText(MyApplication.getContext(), "修改成功", Toast.LENGTH_SHORT).show();

        } else {
            Toast.makeText(MyApplication.getContext(), "用户不存在", Toast.LENGTH_SHORT).show();
        }
//批量更新
//        List<UserBean> userList = userBeanDao.queryBuilder().where(UserBeanDao.Properties.Name.eq(prevName)).build().list();
//        Log.e("userList", userList + "");
//        if (userList.isEmpty()) {
//            Toast.makeText(MyApplication.getContext(), "用户不存在", Toast.LENGTH_SHORT).show();
//        } else {
//            for (UserBean user : userList) {
//                user.setName(newName);
//                userBeanDao.update(user);
//                Log.e("修改", "修改成功");
//            }
//            Toast.makeText(MyApplication.getContext(), "更新成功", Toast.LENGTH_SHORT).show();
//        }

        mNewNameET.setText("");
        mNameET.setText("");
        mUserList.clear();
        mUserList.addAll(userBeanDao.queryBuilder().build().list());
        mUserAdapter.notifyDataSetChanged();
    }

单个更新
更新单条.gif

批量更新

批量更新.gif

查询

   private void initData() {
        mUserList = GreenDaoManager.getInstance().getSession().getUserBeanDao().queryBuilder().build().list();
        mUserAdapter = new UserAdapter(this, mUserList);
        mUserLV.setAdapter(mUserAdapter);
    }

没错,就是这么愉快的就搞定了~~~

等等,好像有一个问题,当我们升级数据库版本时,发现,以前保存的数据不见了,啊啊啊~~~咋个搞,,我们去查看代码,发现在DaoMaster.java中有这个一段更新代码

    /** WARNING: Drops all table on Upgrade! Use only during development. */
    public static class DevOpenHelper extends OpenHelper {
        public DevOpenHelper(Context context, String name) {
            super(context, name);
        }

        public DevOpenHelper(Context context, String name, CursorFactory factory) {
            super(context, name, factory);
        }

        @Override
        public void onUpgrade(Database db, int oldVersion, int newVersion) {
            Log.i("greenDAO", "Upgrading schema from version " + oldVersion + " to " + newVersion + " by dropping all tables");
            dropAllTables(db, true);
            onCreate(db);
        }
    }

}

q其中的注释WARNING: Drops all table on Upgrade! Use only during development.
翻译过来就是:
警告:删除所有升级的表!仅在开发过程中使用。

这可咋个搞,于是在网上找到了一种解决方案:将以前的表变为临时表,然后创建新表,将临时表中的数据拷贝过来,删除临时表。
就是自己重新添加一个新类继承DaoMaster.OpenHelper,然后自己重新实现onUpgrade方法,
代码如下:
MySQLiteOpenHelper.java

public class MySQLiteOpenHelper extends DaoMaster.OpenHelper {

    public MySQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory) {
        super(context, name, factory);
    }

    /**
     * 数据库升级
     *
     * @param db
     * @param oldVersion
     * @param newVersion
     */
    @Override
    public void onUpgrade(Database db, int oldVersion, int newVersion) {
        //操作数据库的更新
        MigrationHelper.migrate(db,UserBeanDao.class,UserBean1Dao.class,UserBean3Dao.class,SiteDao.class);
    }

}

MigrationHelper.java

public class MigrationHelper {

    /**
     * 调用升级方法
     * @param db
     * @param daoClasses 一系列dao.class
     */
    public static void migrate(Database db, Class<? extends AbstractDao<?, ?>>... daoClasses) {
        //1 新建临时表
        generateTempTables(db, daoClasses);
        //2 创建新表
        createAllTables(db, false, daoClasses);
        //3 临时表数据写入新表,删除临时表
        restoreData(db, daoClasses);
    }


    /**
     * 生成临时表,存储旧的表数据
     * @param db
     * @param daoClasses
     */
    private static void generateTempTables(Database db, Class<? extends AbstractDao<?, ?>>... daoClasses) {
        //方法2
        for (int i=0;i<daoClasses.length;i++){
            DaoConfig daoConfig = new DaoConfig(db,daoClasses[i]);
            String tableName = daoConfig.tablename;
            if (!checkTable(db,tableName))
                continue;
            String tempTableName = daoConfig.tablename.concat("_TEMP");
            StringBuilder insertTableStringBuilder = new StringBuilder();
            insertTableStringBuilder.append("alter table ")
                    .append(tableName)
                    .append(" rename to ")
                    .append(tempTableName)
                    .append(";");
            db.execSQL(insertTableStringBuilder.toString());
        }
    }

    /**
     * 检测table是否存在
     * @param db
     * @param tableName
     */
    private static Boolean checkTable(Database db,String  tableName){
        StringBuilder query = new StringBuilder();
        query.append("SELECT count(*) FROM sqlite_master WHERE type='table' AND name='").append(tableName).append("'");
        Cursor c = db.rawQuery(query.toString(), null);
        if (c.moveToNext()){
            int count = c.getInt(0);
            if(count>0){
                return true;
            }
            return false;
        }
        return false;
    }

    /**
     * 删除所有旧表
     * @param db
     * @param ifExists
     * @param daoClasses
     */
    private static void dropAllTables(Database db, boolean ifExists, @NonNull Class<? extends AbstractDao<?, ?>>... daoClasses) {
        reflectMethod(db, "dropTable", ifExists, daoClasses);
    }

    /**
     * 创建新的表结构
     * @param db
     * @param ifNotExists
     * @param daoClasses
     */
    private static void createAllTables(Database db, boolean ifNotExists, @NonNull Class<? extends AbstractDao<?, ?>>... daoClasses) {
        reflectMethod(db, "createTable", ifNotExists, daoClasses);
    }

    /**
     * 创建根删除都在NoteDao声明了,可以直接拿过来用
     * dao class already define the sql exec method, so just invoke it
     */
    private static void reflectMethod(Database db, String methodName, boolean isExists, @NonNull Class<? extends AbstractDao<?, ?>>... daoClasses) {
        if (daoClasses.length < 1) {
            return;
        }
        try {
            for (Class cls : daoClasses) {
                //根据方法名,找到声明的方法
                Method method = cls.getDeclaredMethod(methodName, Database.class, boolean.class);
                method.invoke(null, db, isExists);
            }
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    /**
     * 临时表的数据写入新表
     * @param db
     * @param daoClasses
     */
    private static void restoreData(Database db, Class<? extends AbstractDao<?, ?>>... daoClasses) {
        for (int i = 0; i < daoClasses.length; i++) {
            DaoConfig daoConfig = new DaoConfig(db, daoClasses[i]);
            String tableName = daoConfig.tablename;
            String tempTableName = daoConfig.tablename.concat("_TEMP");
            if (!checkTable(db,tempTableName))
                continue;
            // get all columns from tempTable, take careful to use the columns list
            List<String> columns = getColumns(db, tempTableName);
            //新表,临时表都包含的字段
            ArrayList<String> properties = new ArrayList<>(columns.size());
            for (int j = 0; j < daoConfig.properties.length; j++) {
                String columnName = daoConfig.properties[j].columnName;
                if (columns.contains(columnName)) {
                    properties.add(columnName);
                }
            }
            if (properties.size() > 0) {
                final String columnSQL = TextUtils.join(",", properties);

                StringBuilder insertTableStringBuilder = new StringBuilder();
                insertTableStringBuilder.append("INSERT INTO ").append(tableName).append(" (");
                insertTableStringBuilder.append(columnSQL);
                insertTableStringBuilder.append(") SELECT ");
                insertTableStringBuilder.append(columnSQL);
                insertTableStringBuilder.append(" FROM ").append(tempTableName).append(";");
                db.execSQL(insertTableStringBuilder.toString());
            }
            StringBuilder dropTableStringBuilder = new StringBuilder();
            dropTableStringBuilder.append("DROP TABLE ").append(tempTableName);
            db.execSQL(dropTableStringBuilder.toString());
        }
    }

    private static List<String> getColumns(Database db, String tableName) {
        List<String> columns = null;
        Cursor cursor = null;
        try {
            cursor = db.rawQuery("SELECT * FROM " + tableName + " limit 0", null);
            if (null != cursor && cursor.getColumnCount() > 0) {
                columns = Arrays.asList(cursor.getColumnNames());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null)
                cursor.close();
            if (null == columns)
                columns = new ArrayList<>();
        }
        return columns;
    }
}

创建数据库的方式变更为:

 //创建一个数据库

        MySQLiteOpenHelper helper = new MySQLiteOpenHelper(MyApplication.getContext(), "greendao-db", null);
        DaoMaster mDaoMaster = new DaoMaster(helper.getWritableDatabase());
        mDaoSession = mDaoMaster.newSession();

参考:https://github.com/yuweiguocn...


木木木木头脑袋
61 声望4 粉丝