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

在这里记录下最基本的用法,官网上可了解相关的介绍。

1、下载OrmLite jar

  在下载android的:ormlite-android-4.48.jar和ormlite-core-4.48.jar,放在你项目的libs目录下。

2、编写Bean类

package com.example.ormlite.bean;

import java.util.Collection;

import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.field.ForeignCollectionField;
import com.j256.ormlite.table.DatabaseTable; @DatabaseTable(tableName="tb_user") //表明这是数据库中的一张表
public class User {
@DatabaseField(generatedId=true) //generatedId表示id为主键切自动生成
private int id;
@DatabaseField(columnName="name") //columnName的值为该字段在数据库表中的列名
private String name;
@DatabaseField(columnName="desc")
private String desc; public User() { }
public User(String name, String desc) {
this.name = name;
this.desc = desc;
}
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;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", desc=" + desc + "]";
}
}

3.编写Dao类

package com.example.ormlite.db;

import java.sql.SQLException;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory; import com.example.ormlite.bean.User;
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
/**
* 编写DAO类
* 原生的数据库操作,需要继承SQLiteOpenHelper,这里我们需要继承OrmLiteSqliteOpenHelper(间接继承了SQLiteOpenHelper)
* @author Administrator
*
*/
public class DatabaseHelper extends OrmLiteSqliteOpenHelper{
private static final String TABLE_NAME="sqlite-test.db";
private static DatabaseHelper instance; //userDao,每张表对应一个
private Dao<User,Integer> userDao;
public DatabaseHelper(Context context) {
super(context, TABLE_NAME, null, 2);
} /**
* 建表
*/
@Override
public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
try {
TableUtils.createTable(connectionSource, User.class);
} catch (SQLException e) {
e.printStackTrace();
}
} /**
* 更新表
*/
@Override
public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion,
int newVersion) {
try {
TableUtils.dropTable(connectionSource, User.class, true);
onCreate(database, connectionSource);
} catch (SQLException e) {
e.printStackTrace();
}
} /**
* 单例获取该Helper
* @param context
* @return
*/
public static synchronized DatabaseHelper getHelper(Context context){
if(instance==null){
synchronized(DatabaseHelper.class){
if(instance==null){
instance=new DatabaseHelper(context);
}
}
}
return instance;
} /**
* 获得userDao
* 我们会有很多表,每个表一般我们都会单独写个Dao用于操作,这里为了简单我并没有抽取出来,直接写在helper中,比如UserDao的获取
* @return
* @throws SQLException
*/
public Dao<User,Integer> getUserDao() throws SQLException{
if(userDao==null){
userDao=getDao(User.class);
}
return userDao;
} /**
* 释放资源
*/
@Override
public void close() {
super.close();
userDao=null;
}
}

4.测试

关于单元测试的配置可参考Android Junit测试框架

package com.example.ormlite.test;

import java.sql.SQLException;
import java.util.List; import android.test.AndroidTestCase;
import android.util.Log; import com.example.ormlite.bean.User;
import com.example.ormlite.dao.UserDao;
import com.example.ormlite.db.DatabaseHelper; public class OrmLiteDbTest extends AndroidTestCase{
public void testAddUser(){
// User u1=new User("yanqiong", "developer");
// DatabaseHelper helper=DatabaseHelper.getHelper(getContext());
// try {
// helper.getUserDao().create(u1);
// u1=new User("yanqiong2", "developer");
// helper.getUserDao().create(u1);
// u1=new User("yanqiong3", "developer");
// helper.getUserDao().create(u1);
// u1=new User("yanqiong4", "developer");
// helper.getUserDao().create(u1);
//
// testList();
// } catch (SQLException e) {
// e.printStackTrace();
// } User u1=new User("yanqiong5", "developer");
UserDao userDao=new UserDao(getContext());
userDao.add(u1); } public void testList() {
DatabaseHelper helper=DatabaseHelper.getHelper(getContext());
User u1=new User("yanqiong-android", "developer");
u1.setId(2);
List<User> users;
try {
users = helper.getUserDao().queryForAll();
Log.i("OrmLiteDbTest", users.toString());
} catch (SQLException e) {
e.printStackTrace();
}
} public void testDeleteUser(){
DatabaseHelper helper=DatabaseHelper.getHelper(getContext());
try {
helper.getUserDao().deleteById(2);
} catch (SQLException e) {
e.printStackTrace();
}
} public void testUpdateUser(){
DatabaseHelper helper=DatabaseHelper.getHelper(getContext());
User u1=new User("yanqiong-android", "developer");
u1.setId(3);
try {
helper.getUserDao().update(u1);
} catch (SQLException e) {
e.printStackTrace();
}
}
}

规范的写法请参考Android 数据库框架OrmLite的使用(二)

上一篇:2018.08.27 lucky(模拟)


下一篇:Android 开源框架Universal-Image-Loader完全解析(三)---源代码解读