标签:greendao sql 数据库 java android
1: 使用的架包:greenDAO-generator.jar ,freemarker.jar,greenDAO.jarpackage com.example.application; import com.example.db.DaoMaster; import com.example.db.DaoSession; import com.example.db.DaoMaster.DevOpenHelper; import android.app.Application; import android.content.Context; import android.database.sqlite.SQLiteDatabase; public class BaseApplication extends Application { private static BaseApplication mInstance; private static final String DB_NAME = "pp-dbs"; private static DaoMaster mDaoMaster; private static DaoSession mDaoSession; @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); if (mInstance == null) mInstance = this; } public static DaoMaster getDaoMaster(Context context) { if (mDaoMaster == null) { DevOpenHelper openHelper = new DevOpenHelper(context, DB_NAME, null); SQLiteDatabase db = openHelper.getWritableDatabase(); mDaoMaster = new DaoMaster(db); } return mDaoMaster; }; public static DaoSession getDaoSession(Context context) { if (mDaoSession == null) { if (mDaoMaster == null) { getDaoMaster(context); } mDaoSession = mDaoMaster.newSession(); } return mDaoSession; } }
9:数据操作工具类; package com.example.db; import java.util.List; import com.example.application.BaseApplication; import android.content.Context; public class DbService { private final String TAG = DbService. class .getSimpleName(); private static DbService mInstance; private static Context mContext; private DaoSession mDaoSession ; public PersonDao mPersonDao ; private DbService() { } public static DbService getInstance(Context context) { if (mInstance == null) { mInstance = new DbService(); if (mContext == null) { mContext = context.getApplicationContext(); } mInstance .mDaoSession = BaseApplication.getDaoSession(context); mInstance .mPersonDao = mInstance .mDaoSession .getPersonDao(); } return mInstance ; } /** * 获取所有数据 * * @return */ public List<Person> getAllPerson() { return mPersonDao .loadAll(); } /** * 插入数据 * * @param pp * @return */ public long savePerson(Person pp) { return mPersonDao .insert(pp); } /** * 删除数据 * * @param pp */ public void deletePerson(Person pp) { mPersonDao .delete(pp); } public void deletePersonDetail( long id){ mPersonDao .deleteByKey(id); } public void deleteAllData(){ mPersonDao .deleteAll(); } /** * 更新数据 * * @param pp */ public void updatePerson(Person pp) { mPersonDao .update(pp); } /** * 根据id 获取数据 * * @param id * @return */ public Person loadPeron( long id) { return mPersonDao .load(id); } /** * 根据条件查询 */ public List<Person> queryPerson(String where, String... params) { return mPersonDao .queryRaw(where, params); } } 10:测试 package com.example.grendaotest; import java.util.Date; import java.util.Iterator; import java.util.List; import java.util.Random; import com.example.db.DaoMaster; import com.example.db.DaoSession; import com.example.db.DaoMaster.DevOpenHelper; import com.example.db.DbService; import com.example.db.Person; import com.example.db.PersonDao; import com.example.db.PersonDao.Properties; import android.os.Bundle; import android.app.Activity; import android.database.sqlite.SQLiteDatabase; import android.util.Log; import android.view.Menu; public class MainActivity extends Activity { DbService mDbService; private final String TAG = MainActivity.class.getSimpleName(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mDbService = DbService.getInstance(this); // Log.e(TAG, "已经插入数据个数:" + createPersonData().size()); mDbService.deleteAllData(); createPersonData(); // deletePer(); getDataWhere() ; } /** * 删除年龄为100的person */ public void deletePer() { for (Iterator<Person> oo = getData().iterator(); oo.hasNext();) { Person pp = oo.next(); if (pp.getAge().equals("100")) { mDbService.deletePerson(pp); } } getData(); } public List<Person> createPersonData() { for (int i = 0; i < 10; i++) { Person p = new Person(); p.setAge(99 + i + ""); p.setColor("红色"+i); p.setDate(new Date()); p.setName("张三" + i); mDbService.savePerson(p); } getData(); return mDbService.getAllPerson(); } private List<Person> getDataWhere() { System.out.println("**********************************************"); List<Person> dataperson; dataperson = (List<Person>) mDbService.mPersonDao.queryBuilder().where( Properties.Age.between("100", "105"),Properties.Color.eq("红色3")).list(); for (int i = 0; i < dataperson.size(); i++) { Log.e(TAG, dataperson.get(i).getAge()); } System.out.println("**********************************************"); return dataperson; } private List<Person> getData() { System.out.println("一共有" + mDbService.getAllPerson().size() + "条数据"); for (Iterator<Person> ii = mDbService.getAllPerson().iterator(); ii .hasNext();) { Log.e(TAG, "年龄=" + "\n" + ii.next().getAge()); } return mDbService.getAllPerson(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
标签:greendao sql 数据库 java android
原文地址:http://blog.csdn.net/jiaruihua_blog/article/details/45826127