标签:映射关系 准备 jar包 nts public integer upd cep rri
1. Ormlite是什么?
2. 为什么选择Ormlite?
Andorid平台的orm数据库框架有很多:比如greenDao,DBFlow,Relm等,甚至xutil,afinal;每个orm框架各有优缺点,而Ormlite使用起来非常容易上手,效率也还不错,所以我们选择它作为学习的对象。试想想,如果一个类库光配置就需要搞一天,你还会有心情用它么?
3. Ormlite如何使用?
public class MyDbhepler extends OrmLiteSqliteOpenHelper { private static final String TAG = "MyDbhepler"; public MyDbhepler(Context context) { super(context, "ngyb.db", null, 1); } @Override public void onCreate(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource) { try { Log.e(TAG, "onCreate: "); TableUtils.createTable(connectionSource, Student.class); } catch (SQLException e) { e.printStackTrace(); } } @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource, int i, int i1) { } }
@DatabaseTable(tableName = "student") public class Student { public Student() { //javabean 一定要创建一个无参数的构造 } public Student(String name) { this.name = name; } public Student(String name, int age) { this.name = name; this.age = age; } @DatabaseField(generatedId = true) public int id; @DatabaseField public String name; @DatabaseField public int age; @Override public String toString() { return "Student{" + "id=" + id + ", name=‘" + name + ‘\‘‘ + ", age=" + age + ‘}‘; } }
MyDbhepler myDbhepler = new MyDbhepler(getApplicationContext()); try { Dao<Student, Integer> dao = myDbhepler.getDao(Student.class); Log.e(TAG, "onCreate: "); dao.create(new Student("ngyb", 18)); // Student student = dao.queryForId(0); Student student = dao.queryForAll().get(0); if (student != null) { student.name = "ltz"; student.age = 23; dao.update(student); } // dao.deleteById(3); List<Student> students = dao.queryForAll(); for (Student student1 : students) { Log.e(TAG, "onCreate: " + student1.toString()); } } catch (SQLException e) { e.printStackTrace(); }
}
标签:映射关系 准备 jar包 nts public integer upd cep rri
原文地址:https://www.cnblogs.com/nangongyibin/p/10421708.html