首先先建一个类继承<span style="font-family: Arial, Helvetica, sans-serif;">OrmLiteSqliteOpenHelper 实现几个方法</span>
/**
* Created by 11 on 2016/5/10.
*/
public class DataBaseHeler extends OrmLiteSqliteOpenHelper {
private final static String DATABASE_NAME = "mydb.db";//db.数据库名
private final static int VERSION = 1;//数据库版本
private Map<String, Dao> maps = new HashMap<>();
//使用单例模式进行数据库访问
private static DataBaseHeler instance;
public static synchronized DataBaseHeler getInstance(Context context) {
if (instance == null) {
synchronized (DataBaseHeler.class) {
if (instance == null) {
instance = new DataBaseHeler(context);
}
}
}
return instance;
}
//获取数据库的访问对象
public synchronized Dao getDao(Class cls) throws SQLException {
Dao dao = null;
String className = cls.getSimpleName();
if (maps.containsKey(className)) {
dao = maps.get(className);
} else {
dao = super.getDao(cls);
maps.put(className, dao);
}
return dao;
}
/*
关闭所有操作
*/
public void close() {
super.close();
for (String key : maps.keySet()) {
Dao dao = maps.get(key);
dao = null;
}
}
public DataBaseHeler(Context context) {
super(context, DATABASE_NAME, null, VERSION);
}
@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);
} catch (SQLException e) {
e.printStackTrace();
}
}
一个java对象用户保存
<pre name="code" class="java">/**
* Created by 11 on 2016/5/10.
*/
@DatabaseTable(tableName = "user_info")
public class User {
@DatabaseField(generatedId = true)
private int id;//generatedId = true 表示数据库的主键
@DatabaseField(columnName = "name")
private String name;//(olumnName对应列的一个名称
@DatabaseField(columnName = "desc")
private String desc;//columnName对应列的一个名称
public User() {
}
public User(int id, String name, String desc) {
this.id = id;
this.name = name;
this.desc = desc;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}
一些封装的增删改查的方法
</pre><pre name="code" class="java"><pre name="code" class="java">/**
* Created by 11 on 2016/5/10.
*/
public class UserDao {
private Context context;
private Dao<User,Integer>userDao;
private DataBaseHeler heler;
public UserDao(Context context){
this.context=context;
heler=DataBaseHeler.getInstance(context);
try {
userDao=heler.getDao(User.class);
} catch (SQLException e) {
e.printStackTrace();
}
}
public void addUser(User user){
try {
userDao.create(user);
} catch (SQLException e) {
e.printStackTrace();
}
}
public void updateUser(User user){
try {
userDao.update(user);
} catch (SQLException e) {
e.printStackTrace();
}
}
/*
修改数据 update user_info set name=xx where id=xx
*/
public void upgateUserByID(User user,Integer id){
try {
userDao.updateId(user,id);
} catch (SQLException e) {
e.printStackTrace();
}
}
/*
修改数据 update user_info set name=xx where id=xx
*/
public void updateUserByBuilder(User user){
try {
UpdateBuilder builder=userDao.updateBuilder();
builder.updateColumnValue("name",user.getName()).where().eq("id",1);
builder.update();
} catch (SQLException e) {
e.printStackTrace();
}
}
/*
删除用户
*/
public void deleteUser(User user){
try {
userDao.delete(user);
} catch (SQLException e) {
e.printStackTrace();
}
}
/*
删除全部
*/
public void deleteMuluser(List<User>users){
try {
userDao.delete(users);
} catch (SQLException e) {
e.printStackTrace();
}
}
/*
删除ID
*/
public void deleteUserByIDs(List<Integer>ids){
try {
userDao.deleteIds(ids);
} catch (SQLException e) {
e.printStackTrace();
}
}
/*
查询全部操作
*/
public List<User>listAll(){
try {
return userDao.queryForAll();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
/*
条件查询
*/
public List<User>queryBuilderl(){
List<User>list=null;
QueryBuilder<User,Integer>queryBuilder=userDao.queryBuilder();
//声明一下where条件
Where<User,Integer>where=queryBuilder.where();
try {
where.eq("name","我的名字");//条件名字为赵四
where.and();//和
where.eq("desc","黑龙江");//出生地是黑龙江
where.prepare();
list=queryBuilder.query();
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
}