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

}

最新文章

  1. 玩转spring boot——结合JPA事务
  2. Xcode免证书调试
  3. YTU 2345: 后序遍历二叉树
  4. 介绍开源的.net通信框架NetworkComms框架之八 UDP通信
  5. 30分钟入门Java8之方法引用
  6. PHP导出excel文件
  7. Python练习题 029:Project Euler 001:3和5的倍数
  8. Oracle.ManagedDataAccessDTC.dll 使用
  9. power desinger 学习笔记&lt;二&gt;
  10. Apache配置完虚拟主机后,使用Chrome访问localhost还是默认目录htdocs
  11. 【servlet】 过滤器模板
  12. Mybatis 示例之 Association - 偶尔记一下 - 博客频道 - CSDN.NET
  13. 3624: [Apio2008]免费道路
  14. Java8新特性之三:Stream API
  15. Linux C 实现一个简单的线程池
  16. 解决docker多开mysql报错问题
  17. DNS基础
  18. imrersize函数
  19. OpenCV——图像的深度与通道数讲解
  20. zabbix3.0 安装时出现PHP Parse error: syntax error

热门文章

  1. PHP 实例 - AJAX 实时搜索
  2. PHP AJAX 简介
  3. Java内存泄漏分析系列之一:使用jstack定位线程堆栈信息
  4. RunLoop总结:RunLoop基础知识
  5. GCT学习总结
  6. 两种利用GCD实现分步获取结果的方式和SDWebImage缓存机制的验证
  7. shell编程--基本格式,基本语法,运算符,expr,(()),$[]
  8. protobuf中的枚举缺省值应该为UNKNOWN
  9. Github上的Android项目介绍之ListViewAnimation(针对listView item的侧滑菜单)(1)
  10. ACE在Linux下编译安装