1、在开发中,如果需要使用到数据库,使用android的Sqlite就需要有点小麻烦,这里推荐一下ormlite数据库的使用,用法和sqlite类似,但是操作简单方便
首先,需要导入jar包或者添加依赖,和SQLite一样,同样需要常见dbhelper类,但是这几继承的是OrmLiteSqliteOpenHelper,重写构造方法,和创建表和销毁表的方法
public class PersonDBHelper extends OrmLiteSqliteOpenHelper { private static final String TABLE_DB_NAME = "person_01"; private static final int VERSON_DB = 1; private static PersonDBHelper mInstance; //用一个集合来存放dao Map<String, Dao> daoMap = new HashMap<>(); //和sqliter一样,改造构造方法,采用单例设计模式 private PersonDBHelper(Context context) { super(context, TABLE_DB_NAME, null, VERSON_DB); } public static PersonDBHelper getPersonDBhelper(Context context) { if (mInstance == null) { synchronized (PersonDBHelper.class) { if (mInstance == null) { mInstance = new PersonDBHelper(context); } } } return mInstance; } //更新表的时候调用 @Override public void onCreate(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource) { try {
//这里就不需要些sql语句了,直接调用工具类的静态方法创建表 TableUtils.createTable(connectionSource, Person.class); } catch (SQLException e) { e.printStackTrace(); } } //销毁表,或者更新表的时候调用 @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource, int i, int i1) { try { if (i1 > i) { TableUtils.dropTable(connectionSource, Person.class, true); } } catch (SQLException e) { e.printStackTrace(); } } //创建一个方法获取中间的DAO层 public synchronized Dao getHelperDao(Class clazz) throws SQLException { String name = clazz.getSimpleName(); if (!TextUtils.isEmpty(name)) { Dao dao = getDao(clazz); daoMap.put(name, dao); return dao; } else { return null; } } //创建一个方法用于释放资源 public void closeRes() { close(); for (String key : daoMap.keySet()) { Dao dao = daoMap.get(key); //释放资源 if (dao != null) dao = null; } } }
其次,需要创建表对应的字段,sqlite都写在sql语句中,但是ormlite就需要自己写字段
@DatabaseTable(tableName = "orm_person") public class Person { //自增的id @DatabaseField(columnName = "id",useGetSet = true) private int id; //姓名一列 @DatabaseField(columnName = "name",canBeNull = false) private String name; //年龄一列 @DatabaseField(columnName = "age",canBeNull = true) private int age; @DatabaseField(columnName = "gender",canBeNull = false) private String gender; public Person(int id, String name, int age, String gender) { this.id = id; this.name = name; this.age = age; this.gender = gender; } public Person(){} 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 int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } @Override public String toString() { return "Person{" + "编号=" + id + ", 姓名='" + name + '\'' + ", 年龄=" + age + ", 性别='" + gender + '\'' + '}'; } }
然后,就需要自己创建中间dao层来处理增、删、改、查的方法,以下只列举出部分增,删,
public class PersonDao { private PersonDBHelper mHelper; private Dao helperDao; public PersonDao(Context context) { try { mHelper = PersonDBHelper.getPersonDBhelper(context); helperDao = mHelper.getDao(Person.class); } catch (SQLException e) { e.printStackTrace(); } } //接下来就是提供增删改查的方法 //增加 public void addOne(Person p) { try { helperDao.create(p); } catch (SQLException e) { e.printStackTrace(); } } //增加集合 public void adddeList(ArrayList<Person> list) { try { helperDao.create(list); } catch (SQLException e) { e.printStackTrace(); } } //删除一个 public void delete(Person p) { try { helperDao.delete(p); } catch (SQLException e) { e.printStackTrace(); } } //查找所有 public List<Person> queryAll() { List<Person> list = null; try { list = helperDao.queryForAll(); } catch (SQLException e) { e.printStackTrace(); return null; } return list; } }
使用:创建dao层对象,调用增、删、改、查的方法即可,插入一个集合
PersonDao dao = new PersonDao(this); ArrayList<Person> list = new ArrayList<>(); for (int i = 0; i <4 ; i++) { Person p = new Person(i,"张" +i,12+i,"nv"); list.add(p); }
//调用添加集合的方法 dao.adddeList(list);
//查询所有,是否添加成功 List<Person> persons = dao.queryAll(); if (persons !=null && persons.size()>0){ String text = ""; for (int i = 0; i <persons.size() ; i++) { text =text +persons.get(i).toString(); }
//添加成功显示 showData.setText(text); }else{ showData.setText("出错了,没有拿到,为空"); } }
说道这里,大家应该都有了进一步的了解,谢谢!