ormlite的使用

ormlite的使用

 第一步:
下载ormlite-android-4.41.jar和ormlite-core-4.41.jar两个jar包,放入工程的libs文件夹内,然后右键 builder path-->add path
第二步:
对你要持久化的类进行注解(记住.提供一个默认无参构造)
代码:
/**
 * 建表(表名)
 */
@DatabaseTable(tableName = "students")
public class Student {
	
	/**
	 * 属性(主键)generatedId=true
	 */
	@DatabaseField(id = true)
	private int id;
	/**
	 * 属性
	 */
	@DatabaseField
	private String name;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}

第三步:
创建一个database helper类,继承OrmLiteSqliteOpenHelper 实现 onCreate 和 onUpgrade
代码:
public class DatabaseOpenHelper extends OrmLiteSqliteOpenHelper {

	// 单例模式 一个静态常量
	private static DatabaseOpenHelper helper;

	// 静态方法
	public static DatabaseOpenHelper getInstance(Context context) {
		synchronized (DatabaseOpenHelper.class) {

			if (helper == null) {
				synchronized (DatabaseOpenHelper.class) {
					if (helper == null) {
						helper = new DatabaseOpenHelper(context);
					}
				}
			}
		}
		return helper;
	}

	private static String DB_NAME = "JREDU.DB";
	private static int DB_VERSION = 1;

	// 构造函数
	public DatabaseOpenHelper(Context context) {
		super(context, DB_NAME, null, DB_VERSION);
	}

	// 两个继承方法
	@Override
	public void onCreate(SQLiteDatabase sqlDatabase,
			ConnectionSource connectionSource) {
		try {
			TableUtils.createTable(connectionSource, Student.class);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase sqLiteDatabase,
			ConnectionSource connectionSource, int i, int i1) {

	}

	private Dao<Student, Integer> studentDao = null;

	public Dao<Student, Integer> getStudentDao() {
		if (studentDao == null) {

			try {
				studentDao = getDao(Student.class);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return studentDao;
	}

	@Override
	public void close() {
		super.close();
		studentDao = null;
	}
}
第四步:使用(Activity)
代码:

Dao<Student, Integer> studentDao = 
				DatabaseOpenHelper.getInstance(this).getStudentDao();
Student s=new Student();
s.setId(1);
s.setName("meimei");
		
try {
		studentDao.create(s);//相当于插入一条数据
	} catch (SQLException e) {
		e.printStackTrace();
	}
//查询数据
List<Student> students=studentDao.queryForAll();
StringBuffer sb=new StringBuffer();
for(int i=0;i<students.size();i++){
	sb.append("id:").append(students.get(i).getId())
	.append(" name:").append(students.get(i).getName()).append("\n");
}
show.setText(sb.toString()+" ");
		
//		studentDao.delete(s);//删除数据
s.setName("xiaoxiao");
studentDao.update(s);
students=studentDao.queryForAll();
sb=new StringBuffer();
for(int i=0;i<students.size();i++){
	sb.append("id:").append(students.get(i).getId())
			.append(" name:").append(students.get(i).getName()).append("\n");
}
show.setText(sb.toString()+" ");


网络链接:http://blog.youkuaiyun.com/joker_zhou/article/details/7869244
资源下载:
http://download.youkuaiyun.com/detail/chengtaoyan/9214749


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值