在web开发中经常采用的hibernate,在android也提供了一个ormlite

导入所需jar包后

/**
 * SQLiteHelperOrm.java
 * 版权所有(C) 2014
 * 创建者:cuiran 2014-2-12 下午3:18:40
 */
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;

/**
 * TODO
 * @author cuiran
 * @version 1.0.0
 */
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);
		}
	}

}

/**
 * DbHelper.java
 * 版权所有(C) 2014
 * 创建者:cuiran 2014-2-12 下午3:37:07
 */
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;

/**
 *  DbHelper 数据库操作类
 * @author cuiran
 * @version 1.0.0
 */
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>();
	}

}

新建一个PO

/**
 * POTest.java
 * 版权所有(C) 2014
 * 创建者:cuiran 2014-2-12 下午3:25:08
 */
package com.ghyf.mplay.po;

import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;

/**
 *  POTest DB的测试PO
 * @author cuiran
 * @version 1.0.0
 */
@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的更多相关文章

  1. (转)Android 使用com.j256.ormlite

    在web开发中经常采用的hibernate,在android也提供了一个ormlite 导入所需jar包后 摘自:http://blog.csdn.net/cuiran/article/details ...

  2. 【Android - 框架】之ORMLite的使用

    Android中有很多操作SQLite数据库的框架,现在最常见.最常用的是ORMLite和GreenDAO.ORMLite相比与GreenDAO来说是一个轻量级的框架,而且学习成本相对较低.所以这个帖 ...

  3. Android 快速开发系列 ORMLite 框架最佳实践

    比较靠谱的Helper的写法: 1.DatabaseHelper package com.zhy.zhy_ormlite.db; import java.sql.SQLException; impor ...

  4. Android 快速开发系列 ORMLite 框架最佳实践之实现历史记录搜索

    首先在build.gald中添加compile 'com.j256.ormlite:ormlite-android:4.48'的引用 compile 'com.j256.ormlite:ormlite ...

  5. [ 转]Android快速开发–使用ORMLite操作数据库

    OrmLite是一个数据库操作辅助的开源框架,主要面向Java语言.在Android面向数据库开发中,是一个比较流行的开源框架,方便操作而且功能强大,今天来学习一下,最近的项目中也有所涉及,写个博客来 ...

  6. android数据库持久化框架, ormlite框架,

    前言 Android中内置了SQLite,但是对于数据库操作这块,非常的麻烦.其实可以试用第3方的数据库持久化框架对之进行结构上调整, 摆脱了访问数据库操作的细节,不用再去写复杂的SQL语句.虽然这样 ...

  7. [BUG集] android 安卓项目中ORMLITE框架 Must specify one of id, generatedId, and generatedIdSequence with Id

    使用ORM框架ORMLITE有一段时间,今天在操作一个对象的时候,重新运行报错如下: Must specify one of id, generatedId, and generatedIdSeque ...

  8. Android Ormlite 学习笔记1 -- 基础

    Ormlite 是一个开源Java数据实体映射框架.其中依赖2个核心类库: 1.ormlite-android-4.48.jar 2.ormlite-core-4.48.jar 新建项目,引用上面2个 ...

  9. Android 数据库框架OrmLite的使用(一)

    在这里记录下最基本的用法,官网上可了解相关的介绍. 1.下载OrmLite jar 在下载android的:ormlite-android-4.48.jar和ormlite-core-4.48.jar ...

随机推荐

  1. Android自定义View(CustomCalendar-定制日历控件)

    转载请标明出处: http://blog.csdn.net/xmxkf/article/details/54020386 本文出自:[openXu的博客] 目录: 1分析 2自定义属性 3onMeas ...

  2. Android Studio 中设置代码块自动补齐

    AS中很多提示键,并不如Eclipse中做的好,需要我们自己去自定义.这里以switch...case为例,讲解一下如何设置代码自动补全. 1.进入settings -->  Editor -- ...

  3. 【mybatis深度历险系列】mybatis中的高级映射一对一、一对多、多对多

    学习hibernate的时候,小编已经接触多各种映射,mybatis中映射有到底是如何运转的,今天这篇博文,小编主要来简单的介绍一下mybatis中的高级映射,包括一对一.一对多.多对多,希望多有需要 ...

  4. 在从1到n的正数中1出现的次数

    #include <iostream> using namespace std; int cal1From0ToN(int n) { int pow1 = 1; int pow2 = 10 ...

  5. Python descriptor

    class A: def __init__(self, name): self.name = name def __get__(self, ins, cls): print('call get') i ...

  6. 亲密接触Redis-第一天

    引言 nosql,大规模分布式缓存遍天下,Internet的时代在中国由其走得前沿,这一切归功于我国特色的电商.因此nosql.大数据技术在中国应用的比国外还要前沿.从这一章开始我们将开始进入到真正的 ...

  7. 视频特性TI(时间信息)和SI(空间信息)的计算工具:TIandSI-压缩码流版

    ===================================================== TI(时间信息)和SI(空间信息)计算工具文章列表: 视频特性TI(时间信息)和SI(空间信 ...

  8. 【伯乐在线】最值得阅读学习的 10 个 C 语言开源项目代码

    原文出处: 平凡之路的博客   欢迎分享原创到伯乐头条 伯乐在线注:『阅读优秀代码是提高开发人员修为的一种捷径』http://t.cn/S4RGEz .之前@伯乐头条 曾发过一条微博:『C 语言进阶有 ...

  9. 指令汇C电子市场开发(一) ActionBar的使用

    前话: 在学习开发谷歌电子市场的的时候,我换了一款比较高大上的模拟器--genymotion,首先去genymotion的官网注册下载,然后安装.感觉这款模拟器运行挺快的,哈哈,而且可以直接把应用拖进 ...

  10. 5.1、Android Studio用Logcat编写和查看日志

    Android Studio在Android Monitor中包含了一个logcat的tab,可以打印系统事件,比如垃圾回收发生时,实时打印应用消息. 为了显示需要的信息,你可以创建过滤器,更改需要显 ...