使用 greenDao 框架 操作数据库
0.效果图
步骤:
1.新建java项目,生成实体类等代码.需要的jar包: freemarker-2.3.10.jar , greendao-generator-1.3.1.jar
import java.io.IOException; import de.greenrobot.daogenerator.DaoGenerator; import de.greenrobot.daogenerator.Entity; import de.greenrobot.daogenerator.Schema; public class Test1 { public static void main(String[] args) throws IOException, Exception { //第一个参数用来更新数据库版本号,第二个参数为要生成的DAO类所在包路径 Schema schema = new Schema(1, "de.bvb.db"); //创建表 addNote(schema); //设置生成的文件文件存放的位置 new DaoGenerator().generateAll(schema, "./src"); } private static void addNote(Schema schema) { Entity note = schema.addEntity("Student");//表名 note.addIdProperty();//自增字段 note.addStringProperty("name").notNull(); note.addIntProperty("age"); } }
2.android项目代码 (db包下面代码为java项目生成的)
2.1 MyApplication中对数据库进行初始化的代码
private static DaoMaster daoMaster; private static DaoSession daoSession; //取得DaoMaster public static DaoMaster getDaoMaster(Context context) { if (daoMaster == null) { DaoMaster.OpenHelper helper = new DaoMaster.DevOpenHelper(instance, "dbName", null); //dbName 为数据库名 daoMaster = new DaoMaster(helper.getWritableDatabase()); } return daoMaster; } //取得DaoSession public static DaoSession getDaoSession() { if (daoSession == null) { if (daoMaster == null) { daoMaster = getDaoMaster(instance); } daoSession = daoMaster.newSession(); } return daoSession; }
2.2DBHelper代码
package de.bvb.util; import java.util.List; import android.content.Context; import de.bvb.MyApplication; import de.bvb.db.DaoSession; import de.bvb.db.Student; import de.bvb.db.StudentDao; import de.greenrobot.dao.query.QueryBuilder; public class DBHelper { private static DBHelper instance; private StudentDao studentDao; private DBHelper() { } //单例 public static DBHelper getInstance() { if (instance == null) { instance = new DBHelper(); DaoSession daoSession = MyApplication.getDaoSession(); instance.studentDao = daoSession.getStudentDao(); } return instance; } public void add(Student student) { studentDao.insert(student); } public void update(Student student) { studentDao.update(student); } public void delete(Student student) { studentDao.delete(student); } public List<Student> query() { return studentDao.queryBuilder().list(); } }
3.调用的方法
@Override public void onClick(View v) { Student student = new Student(); switch (v.getId()) { case R.id.btnAdd: student.setName(etName.getText().toString().trim()); student.setAge(Integer.parseInt(etAge.getText().toString().trim())); DBHelper.getInstance().add(student); break; case R.id.btnUpdate: student.setId(Long.parseLong(tvId.getText().toString().trim())); student.setName(etName.getText().toString().trim()); student.setAge(Integer.parseInt(etAge.getText().toString().trim())); DBHelper.getInstance().update(student); break; case R.id.btnDelete: student.setId(Long.parseLong(tvId.getText().toString().trim())); student.setName(etName.getText().toString().trim()); student.setAge(Integer.parseInt(etAge.getText().toString().trim())); DBHelper.getInstance().delete(student); break; case R.id.btnQuery: list = DBHelper.getInstance().query(); adapter.setData(list); break; } }