【GreenDao】 @推荐资料地址: http://blog.csdn.net/wl1769127285/article/details/52369516 按照文档进行一步步的配置。 Dao包是自动生成的;
代码:
public class MainActivity extends AppCompatActivity { private UserDao dao; private EditText edit_name; private EditText edit_id; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); edit_id = (EditText) findViewById(R.id.edit_id); edit_name = (EditText) findViewById(R.id.edit_name); dao = GreenDaoUtils.getSingleTon().getmDaoSession().getUserDao(); } public void onClick(View v) { switch (v.getId()) { case R.id.btn_c: User insertData = new User(null, edit_name.getText().toString(), 21, false); long rowId = dao.insert(insertData); if (rowId > 0) { Toast.makeText(MainActivity.this, "插入成功!", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this, "插入失敗!", Toast.LENGTH_SHORT).show(); } break; case R.id.btn_r: List<User> users = dao.loadAll(); for (int i = 0; i < users.size(); i++) { Log.i("woyaokk", "结果:" + users.get(i).toString()); } break; case R.id.btn_u: List<User> userss = dao.loadAll(); User user = new User(userss.get(0).getId(), edit_name.getText().toString(), 22, true); dao.update(user); break; case R.id.btn_d: String s = edit_id.getText().toString(); long l = Long.parseLong(s); dao.deleteByKey(l); break; } } } =========================================================== public class MyApplication extends Application { static Context context; @Override public void onCreate() { super.onCreate(); context = this; } public static Context getApplication() { return context; } } =========================================================== public class GreenDaoUtils { private DaoMaster.DevOpenHelper mHelper; private SQLiteDatabase db; private DaoMaster mDaoMaster; private DaoSession mDaoSession; private static GreenDaoUtils greenDaoUtils; private GreenDaoUtils(){} public static GreenDaoUtils getSingleTon(){ if (greenDaoUtils==null){ greenDaoUtils=new GreenDaoUtils(); } return greenDaoUtils; } private void initGreenDao(){ mHelper=new DaoMaster.DevOpenHelper(MyApplication.getApplication(),"test-db",null); db=mHelper.getWritableDatabase(); mDaoMaster=new DaoMaster(db); mDaoSession=mDaoMaster.newSession(); } public DaoSession getmDaoSession() { if (mDaoMaster==null){ initGreenDao(); } return mDaoSession; } public SQLiteDatabase getDb() { if (db==null){ initGreenDao(); } return db; } } =========================================================== @Entity public class User { @Id(autoincrement = true) private Long id; private String name; private int age; private boolean isBoy; public boolean getIsBoy() { return this.isBoy; } public void setIsBoy(boolean isBoy) { this.isBoy = isBoy; } public int getAge() { return this.age; } public void setAge(int age) { this.age = age; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public Long getId() { return this.id; } public void setId(Long id) { this.id = id; } @Generated(hash = 1724489812) public User(Long id, String name, int age, boolean isBoy) { this.id = id; this.name = name; this.age = age; this.isBoy = isBoy; } @Generated(hash = 586692638) public User() { } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + ", isBoy=" + isBoy + '}'; } }
运行结果: