快速接入:
第一步:gradle引入
compile ‘com.github.satyan:sugar:1.5’
第二步:manifest文件的application标签下配置
数据库的名字,版本号,log,你的JavaBean所在的包(会自动找到这个包,通过映射关系,给你生成对应的表)
<meta-data android:name="DATABASE" android:value="sugar_example.db" />
<meta-data android:name="VERSION" android:value="2" />
<meta-data android:name="QUERY_LOG" android:value="true" />
<meta-data android:name="DOMAIN_PACKAGE_NAME" android:value="com.example" />
第三步:项目的Application类集成SugarAPP类
public App extends SugarApp{
}
第四步:建表,如下类名就是表名。
注意:
1.驼峰命名的类名最终转化成下划线并小写的表名,如BookRecord(类名)—>book_record(表名)
2.一定要保留一个空的构造函数
public class Book extends SugarRecord {
String title;
String edition;
public Book(){
}
public Book(String title, String edition){
this.title = title;
this.edition = edition;
}
}
第五步:简单用法,CURD如下:
Save Entity:
Book book = new Book("Title here", "2nd edition")
book.save();
Load Entity:
Book book = Book.findById(Book.class, 1);
Update Entity:
Book book = Book.findById(Book.class, 1);
book.title = "updated title here"; // modify the values
book.edition = "3rd edition";
book.save(); // updates the previous entry with new values.
Delete Entity:
Book book = Book.findById(Book.class, 1);
book.delete();
Bulk Operations:
List<Book> books = Book.listAll(Book.class);
Book.deleteAll(Book.class);
Book.find(Book.class, "name = ? and title = ?", "satya", "title1")