android数据库框架SugarORM的简单使用

原文地址:http://satyan.github.io/sugar/getting-started.html

复杂使用地址:http://satyan.github.io/sugar/creation.html

步骤1:下载

Gradle: 

compile 'com.github.satyan:sugar:1.5'

步骤2:配置

AndroidManifest.xml

 android:label="@string/app_name" android:icon="@drawable/icon"
android:name="com.orm.SugarApp">//使用SugarApp的Application
 android:name="DATABASE" android:value="sugar_example.db" />  //数据库名称
 android:name="VERSION" android:value="2" />      //数据库版本号
 android:name="QUERY_LOG" android:value="true" />    //log
 android:name="DOMAIN_PACKAGE_NAME" android:value="com.example" /> //实体类包名
.
.
步骤3:实体类

public class Book extends SugarRecord {
  String title;
  String edition;

  public Book(){
  }

  public Book(String title, String edition){
    this.title = title;
    this.edition = edition;
  }
}
或者:

@Table
public class Book {
  private Long id;

  public Book(){
  }

  public Book(String title, String edition){
     this.title = title;
     this.edition = edition;
  }

  public Long getId() {
      return id;
  }
}
步骤4:基本使用方法

 保存Entity:

Book book = new Book("Title here", "2nd edition")
book.save();
加载 Entity:
Book book = Book.findById(Book.class, 1);
更新 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.
删除 Entity:
Book book = Book.findById(Book.class, 1);
book.delete();
批量操作:
List<Book> books = Book.listAll(Book.class);

Book.deleteAll(Book.class);



你可能感兴趣的:(Android)