Android 使用 com.j256.ormlite 简单使用 【1】

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);
		}
	}

}

  

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;
	}

	/**
	 * 根据特定条件更新特定字段
	 * 
	 * @param c
	 * @param values
	 * @param columnName where字段
	 * @param value where值
	 * @return
	 */
	@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>();
	}
	
}

  

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();
	}
	
	
	
}

  

posted @ 2014-06-07 01:30  lhj588  阅读(3779)  评论(0编辑  收藏  举报