GreenDao深度探索(二)
来源:互联网 发布:淘宝怎么异地发货 编辑:程序博客网 时间:2024/05/21 05:23
前篇已经介绍了基本的配置以及生成dao文件,这一篇将介绍针对数据库的几种操作。
- Application中初始化
private void setupDatabase() { // 通过 DaoMaster 的内部类 DevOpenHelper,你可以得到一个便利的 SQLiteOpenHelper 对象。 // 可能你已经注意到了,你并不需要去编写「CREATE TABLE」这样的 SQL 语句,因为 greenDAO 已经帮你做了。 // 注意:默认的 DaoMaster.DevOpenHelper 会在数据库升级时,删除所有的表,意味着这将导致数据的丢失。 // 所以,在正式的项目中,你还应该做一层封装,来实现数据库的安全升级。 helper = new DaoMaster.DevOpenHelper(this, Constants.DB_NAME, null); db = helper.getWritableDatabase(); // 注意:该数据库连接属于 DaoMaster,所以多个 Session 指的是相同的数据库连接。 daoMaster = new DaoMaster(db); daoSession = daoMaster.newSession(); } public DaoSession getDaoSession() { return daoSession; } public SQLiteDatabase getDb() { return db; }
- 在activity中
1.查找所有
cursor = getDb().query(getNoteDao().getTablename(), getNoteDao().getAllColumns(), null, null, null, null, orderBy); private NoteDao getNoteDao() { // 通过 BaseApplication 类提供的 getDaoSession() 获取具体 Dao return ((BaseApplication) this.getApplicationContext()).getDaoSession().getNoteDao(); } private SQLiteDatabase getDb() { // 通过 BaseApplication 类提供的 getDb() 获取具体 db return ((BaseApplication) this.getApplicationContext()).getDb(); }
2.增加
private void addNote() { String noteText = editText.getText().toString(); editText.setText(""); final DateFormat df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM); String comment = "Added on " + df.format(new Date()); if (noteText == null || noteText.equals("")) { ToastUtils.show(getApplicationContext(), "Please enter a note to add"); } else { // 插入操作,简单到只要你创建一个 Java 对象 Note note = new Note(null, noteText, comment, new Date()); getNoteDao().insert(note); Log.d(TAG, "Inserted new note, ID: " + note.getId()); cursor.requery(); } }
3.查询
private void search() { String noteText = editText.getText().toString(); editText.setText(""); if (noteText == null || noteText.equals("")) { ToastUtils.show(getApplicationContext(), "Please enter a note to query"); } else { // Query 类代表了一个可以被重复执行的查询 Query query = getNoteDao().queryBuilder() .where(NoteDao.Properties.Text.eq(noteText)) .orderAsc(NoteDao.Properties.Date) .build(); // 查询结果以 List 返回 List notes = query.list(); ToastUtils.show(getApplicationContext(), "There have " + notes.size() + " records"); } // 在 QueryBuilder 类中内置两个 Flag 用于方便输出执行的 SQL 语句与传递参数的值 QueryBuilder.LOG_SQL = true; QueryBuilder.LOG_VALUES = true; }
4.删除
getNoteDao().deleteByKey(id);// getNoteDao().deleteAll(); ToastUtils.show(getApplicationContext(), "Deleted note, ID: " + id); Log.d(TAG, "Deleted note, ID: " + id); cursor.requery();
1 0
- GreenDao深度探索(二)
- ZeroMQ 深度探索(二)
- GreenDao初步探索(一 )
- [架构] ZeroMQ 深度探索(二)
- [架构] ZeroMQ 深度探索(二)
- [架构] ZeroMQ 深度探索(二)
- Amazon Aurora深度探索(二)
- 深度探索C++对象模型笔记(二)
- 《深度探索C++对象模型》系列之(二)
- 深度探索C++对象模型笔记(二)
- 深度探索c++对象模型——读书笔记(二)
- 《深度探索C++对象模型》读书笔记(二)
- 《深度探索C++对象模型》读书笔记[二]
- GreenDao详解(二)之在项目中配置GreenDao
- greenDAO讲义(二):数据库查询篇
- greenDAO讲义(二):数据库查询篇
- greenDAO的简单使用(二)
- GreenDAO框架(二)基本使用篇
- 第二周项目5-个人所得税计算机
- Eclipse配置Struts2二
- linux系统ip地址的配置
- 服务端挂盘开启1394调试
- leetcode:Partition List 【Java】
- GreenDao深度探索(二)
- 快速排序的非递归写法
- Android安全教程(2)---Fiddler简易使用教程之使用
- Idiomatic Python, well format in python
- 用grunt搭建自动化的web前端开发环境
- Ubuntu下 AndroidStudio 无法识别设备(手机/平板)的问题 , 多设备可以
- 集成Cordova+crosswalk with Android
- iOS8统一的系统提示控件——UIAlertController
- CXF+Spring 发布SOAP服务