继续接上文:
Android 使用 Greendao 操作 db sqlite(1)-- 直接在MainActivity中调用
布局文件同上文一致,这里就不贴了。
一、封装DaoUtils类
UserInfoUtils.java
package com.jack.testmd.greendao; import com.jack.testmd.model.UserInfo; import java.util.List; public class UserInfoUtils { private UserInfoDao userInfoDao = DBManager.get().getUserInfoDao(); private DaoSession daoSession = DBManager.get().getDaoSession(); /** * 插入单条 * * @param userInfo * @return */ public boolean insert(UserInfo userInfo) { return userInfoDao.insert(userInfo) == -1 ? false : true; } /** * 插入多条 * * @param dataList * @return */ public boolean insertMult(final List<UserInfo> dataList) { boolean flag = false; try { daoSession.runInTx(new Runnable() { @Override public void run() { for (UserInfo data : dataList) { userInfoDao.insertOrReplace(data); } } }); flag = true; } catch (Exception e) { e.printStackTrace(); } return flag; } /** * 修改一条数据 * * @param data * @return */ public boolean update(UserInfo data) { boolean flag = false; try { userInfoDao.update(data); flag = true; } catch (Exception e) { e.printStackTrace(); } return flag; } /** * 删除单条记录 * * @param key * @return */ public boolean deleteByKey(long key) { boolean flag = false; try { //按照id删除 userInfoDao.deleteByKey(key); flag = true; } catch (Exception e) { e.printStackTrace(); } return flag; } /** * 删除所有记录 * * @return */ public boolean deleteAll() { boolean flag = false; try { userInfoDao.deleteAll(); flag = true; } catch (Exception e) { e.printStackTrace(); } return flag; } /** * 查询所有记录 * * @return */ public List<UserInfo> findAll() { return userInfoDao.loadAll(); } /** * 根据主键id查询记录 * * @param key * @return */ public UserInfo findById(long key) { return userInfoDao.load(key); } /** * 使用native sql进行查询操作 */ public List<UserInfo> findBySql(String sql, String[] conditions) { return userInfoDao.queryRaw(sql, conditions); } }
调用代码:
package com.jack.testmd; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import com.jack.testmd.application.MyApplication; import com.jack.testmd.greendao.DBManager; import com.jack.testmd.greendao.UserInfoDao; import com.jack.testmd.greendao.UserInfoUtils; import com.jack.testmd.model.UserInfo; import java.util.List; public class TestGreenActivity extends AppCompatActivity { private final String TAG = DBManager.class.getSimpleName();private UserInfoUtils dbUtils = new UserInfoUtils(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test_green); } protected void optGreen(View v) { switch (v.getId()) { case R.id.btn_all: List<UserInfo> list = dbUtils.findAll(); for (int i = 0; i < list.size(); i++) { Log.i(TAG, "id:" + list.get(i).getId() + ",name:" + list.get(i).getUserName() + ",age:" + list.get(i).getAge()); } break; case R.id.btn_add: UserInfo userInfo = new UserInfo(1, "a001", 10); dbUtils.insert(userInfo); break; case R.id.btn_update: UserInfo userInfo2 = new UserInfo(1, "b001", 10); dbUtils.update(userInfo2); break; case R.id.btn_del: dbUtils.deleteByKey((long) 1); break; case R.id.btn_clear: dbUtils.deleteAll(); break; } } }
本博客地址: wukong1688
本文原文地址:https://www.cnblogs.com/wukong1688/p/10705662.html
转载请著名出处!谢谢~~