在web开发中经常采用的hibernate,在android也提供了一个ormlite
导入所需jar包后
摘自:http://blog.csdn.net/cuiran/article/details/24722885
- package com.ghyf.mplay.database;
-
- import java.sql.SQLException;
-
- import android.content.Context;
- import android.database.sqlite.SQLiteDatabase;
-
- import com.ghyf.mplay.application.BaseCookie;
- import com.ghyf.mplay.po.POPriorityText;
- import com.ghyf.mplay.po.POTest;
- import com.ghyf.mplay.util.LogUtil;
- import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
- import com.j256.ormlite.support.ConnectionSource;
- import com.j256.ormlite.table.TableUtils;
-
-
- public class SQLiteHelperOrm extends OrmLiteSqliteOpenHelper {
- private static final String TAG="SQLiteHelperOrm";
- private static final String DATABASE_NAME = "mplay.db";
- private static final int DATABASE_VERSION = 3;
-
- public SQLiteHelperOrm(Context context) {
- super(context, DATABASE_NAME, null, DATABASE_VERSION);
- }
-
- public SQLiteHelperOrm() {
- super(BaseCookie.getContext(), DATABASE_NAME, null, DATABASE_VERSION);
- }
-
- @Override
- public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {
- try {
- TableUtils.createTable(connectionSource, POTest.class);
- TableUtils.createTable(connectionSource, POPriorityText.class);
- } catch (SQLException e) {
- LogUtil.e(TAG,"onCreate",e);
- }
- }
-
- @Override
- public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int arg2, int arg3) {
- try {
- TableUtils.dropTable(connectionSource, POTest.class, true);
- TableUtils.dropTable(connectionSource, POPriorityText.class, true);
- onCreate(db, connectionSource);
- } catch (SQLException e) {
- LogUtil.e(TAG,"onUpgrade",e);
- }
- }
-
- }
- package com.ghyf.mplay.database;
-
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Map;
-
- import android.annotation.TargetApi;
- import android.content.ContentValues;
- import android.os.Build;
-
- import com.ghyf.mplay.util.LogUtil;
- import com.j256.ormlite.dao.Dao;
- import com.j256.ormlite.stmt.UpdateBuilder;
-
-
- public class DbHelper<T> {
- private static final String TAG="DbHelper";
-
- public int create(T po) {
- SQLiteHelperOrm db = new SQLiteHelperOrm();
- try {
- Dao dao = db.getDao(po.getClass());
- return dao.create(po);
- } catch (SQLException e) {
- LogUtil.e(TAG,"create",e);
- } finally {
- if (db != null)
- db.close();
- }
- return -1;
- }
-
- public boolean exists(T po, Map<String, Object> where) {
- SQLiteHelperOrm db = new SQLiteHelperOrm();
- try {
- Dao dao = db.getDao(po.getClass());
- if (dao.queryForFieldValues(where).size() > 0) {
- return true;
- }
- } catch (SQLException e) {
- LogUtil.e(TAG,"exists",e);
- } finally {
- if (db != null)
- db.close();
- }
- return false;
- }
-
- public int createIfNotExists(T po, Map<String, Object> where) {
- SQLiteHelperOrm db = new SQLiteHelperOrm();
- try {
- Dao dao = db.getDao(po.getClass());
- if (dao.queryForFieldValues(where).size() < 1) {
- return dao.create(po);
- }
- } catch (SQLException e) {
- LogUtil.e(TAG,"createIfNotExists",e);
- } finally {
- if (db != null)
- db.close();
- }
- return -1;
- }
-
-
- public List<T> queryForEq(Class<T> c, String fieldName, Object value) {
- SQLiteHelperOrm db = new SQLiteHelperOrm();
- try {
- Dao dao = db.getDao(c);
- return dao.queryForEq(fieldName, value);
- } catch (SQLException e) {
- LogUtil.e(TAG,"queryForEq",e);
- } finally {
- if (db != null)
- db.close();
- }
- return new ArrayList<T>();
- }
-
-
- public int remove(T po) {
- SQLiteHelperOrm db = new SQLiteHelperOrm();
- try {
- Dao dao = db.getDao(po.getClass());
- return dao.delete(po);
- } catch (SQLException e) {
- LogUtil.e(TAG,"remove",e);
- } finally {
- if (db != null)
- db.close();
- }
- return -1;
- }
-
-
- @TargetApi(Build.VERSION_CODES.HONEYCOMB)
- public int update(Class<T> c, ContentValues values, String columnName, Object value) {
- SQLiteHelperOrm db = new SQLiteHelperOrm();
- try {
- Dao dao = db.getDao(c);
- UpdateBuilder<T, Long> updateBuilder = dao.updateBuilder();
- updateBuilder.where().eq(columnName, value);
- for (String key : values.keySet()) {
- updateBuilder.updateColumnValue(key, values.get(key));
- }
- return updateBuilder.update();
- } catch (SQLException e) {
- LogUtil.e(TAG,"update",e);
- } finally {
- if (db != null)
- db.close();
- }
- return -1;
- }
-
-
- public int update(T po) {
- SQLiteHelperOrm db = new SQLiteHelperOrm();
- try {
-
- Dao dao = db.getDao(po.getClass());
- return dao.update(po);
- } catch (SQLException e) {
- LogUtil.e(TAG,"update",e);
- } finally {
- if (db != null)
- db.close();
- }
- return -1;
- }
-
-
- public List<T> queryForAll(Class<T> c) {
- SQLiteHelperOrm db = new SQLiteHelperOrm();
- try {
- Dao dao = db.getDao(c);
- return dao.queryForAll();
- } catch (SQLException e) {
- LogUtil.e(TAG,"queryForAll",e);
- } finally {
- if (db != null)
- db.close();
- }
- return new ArrayList<T>();
- }
-
- }
新建一个PO
- package com.ghyf.mplay.po;
-
- import com.j256.ormlite.field.DatabaseField;
- import com.j256.ormlite.table.DatabaseTable;
-
- @DatabaseTable(tableName = "test")
- public class POTest {
-
- @DatabaseField(generatedId = true)
- public long _id;
-
- @DatabaseField
- public String title;
-
- @DatabaseField
- public int position;
- public POTest(long _id, String title, int position) {
- super();
- this._id = _id;
- this.title = title;
- this.position = position;
- }
- public POTest() {
- super();
- }
-
-
-
- }
(转)Android 使用com.j256.ormlite
原文:http://www.cnblogs.com/antyi/p/4629725.html