GreenDao数据库依赖添加完了,表也创建好了,下一步就是创建数据库和使用数据库了。
一、创建数据库
DaoMaster.DevOpenHelper devOpenHelper=new DaoMaster.DevOpenHelper(context,"test.db");
SQLiteDatabase database = devOpenHelper.getWritableDatabase();
DaoSession daoSession = new DaoMaster(database).newSession();
QuestionDao questionDao = daoSession.getQuestionDao();
这样一个数据库就生成了,当我们获取
questionDao
对象之后,就能非常方便的操作数据库了。
我这里是通过创建一个单例来管理数据库:
/** * 数据库操作类 * Created by KingsleyCheng on 2017/5/11. */ public class GreenDaoUtils { private MySQLiteOpenHelper mHelper; private SQLiteDatabase db; private DaoMaster mDaoMaster; private DaoSession mDaoSession; private static class GreenDaoUtilsHolder { private static final GreenDaoUtils INSTANCE = new GreenDaoUtils(); } private GreenDaoUtils() { } public static GreenDaoUtils getSingleton() { return GreenDaoUtilsHolder.INSTANCE; } private void initGreenDao() { mHelper = new MySQLiteOpenHelper(BaseApplication.getApplication(), "test-db", null); db = mHelper.getWritableDatabase(); mDaoMaster = new DaoMaster(db); mDaoSession = mDaoMaster.newSession(); } public DaoSession getDaoSession() { if (mDaoMaster == null) { initGreenDao(); } return mDaoSession; }
/** * 获取可写数据库
*/
public SQLiteDatabase
getDb() {
if (
db ==
null) { initGreenDao()
;
}
return
db
;
}
}MySQLiteOpentHelper是为了解决数据库升级数据丢失自定义的一个类,继承于DoMaster.OpenHelper类(会单独介绍的):
public class MySQLiteOpenHelper extends DaoMaster.OpenHelper {数据库管理者单例构建完成后,在application里面注册数据库:
@Override public void onCreate() { super.onCreate(); baseApplication = this; GreenDaoUtils. getSingleton();//数据库初始化 }这样数据库就构建完成了。
二、数据库的基本操作:
mUser = new User((long)2,"anye3");
mUserDao.insert(mUser);//添加一个
mUserDao.deleteByKey(id);
mUser = new User((long)2,"anye0803");
mUserDao.update(mUser);
List users = mUserDao.loadAll();
String userName = "";
for (int i = 0; i < users.size(); i++) {
userName += users.get(i).getName()+",";
}
mContext.setText("查询全部数据==>"+userName);
这些方法都可以写在数据库管理单例里GreenDaoUtils面:
/** * 插入一条记录 * * @param user */ public void insertUser(User user) { DaoMaster daoMaster = new DaoMaster(getWritableDatabase()); DaoSession daoSession = daoMaster.newSession(); UserDao userDao = daoSession.getUserDao(); userDao.insert(user); } /** * 插入用户集合 * * @param users */ public void insertUserList(List当然也可以使用insertOrReplace代替insert,更多的方法大家可以自己查询资料或者查询源码来研究。users) { if (users == null || users.isEmpty()) { return; } DaoMaster daoMaster = new DaoMaster(getWritableDatabase()); DaoSession daoSession = daoMaster.newSession(); UserDao userDao = daoSession.getUserDao(); userDao.insertInTx(users); } /** * 删除一条记录 * * @param user */ public void deleteUser(User user) { DaoMaster daoMaster = new DaoMaster(getWritableDatabase()); DaoSession daoSession = daoMaster.newSession(); UserDao userDao = daoSession.getUserDao(); userDao.delete(user); } /** * 更新一条记录 * * @param user */ public void updateUser(User user) { DaoMaster daoMaster = new DaoMaster(getWritableDatabase()); DaoSession daoSession = daoMaster.newSession(); UserDao userDao = daoSession.getUserDao(); userDao.update(user); } /** * 查询用户列表 */ public List queryUserList() { DaoMaster daoMaster = new DaoMaster(getReadableDatabase()); DaoSession daoSession = daoMaster.newSession(); UserDao userDao = daoSession.getUserDao(); QueryBuilder qb = userDao.queryBuilder(); List list = qb.list(); return list; } /** * 查询用户列表 */ public List queryUserList(int age) { DaoMaster daoMaster = new DaoMaster(getReadableDatabase()); DaoSession daoSession = daoMaster.newSession(); UserDao userDao = daoSession.getUserDao(); QueryBuilder qb = userDao.queryBuilder(); qb.where(UserDao.Properties.Age.gt(age)).orderAsc(UserDao.Properties.Age); List list = qb.list(); return list; }
void addUserList(List还可以使用SQL语句查询:lists) { getDaoSession().getUserListDao().insertOrReplaceInTx(lists); }
cursor = GreenDaoUtils. getSingleton().getDb().rawQuery("SQL语句", null);二、使用数据库查询数据,并获取数据(我的项目需求使用的是SQL语句查询的):
try { // cursor = mDaoSession.getDatabase().rawQuery(SQLValues.SQL_PROJECT, new String[]{Preferences.getUserID()}); cursor = GreenDaoUtils.getSingleton().getDb() .rawQuery(SQLValues.SQL_PROJECT, new String[]{Preferences.getUserID()}); while (cursor.moveToNext()) { projectList = new ProjectList(); projectList.setProjectId(cursor.getString( cursor.getColumnIndex("projectId"))); projectList.setProjectName(cursor.getString( cursor.getColumnIndex("projectName"))); projectLists.add(projectList); } projectMessage.setProjectList(projectLists); setListView(projectMessage); } catch (RuntimeException e) { Log.d("SlidingMyProjectListFra", e.getMessage()); } finally { if (cursor != null) { cursor.close(); } }大家可以根据自己项目需求灵活运用。