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

本文介绍了如何使用 Sugar ORM 在 Android 应用中实现数据库管理。包括安装配置、定义实体类、基本的 CRUD 操作等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文地址: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

<application android:label="@string/app_name" android:icon="@drawable/icon"
android:name="com.orm.SugarApp">//使用SugarApp的Application
<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" />    //log
<meta-data android:name="DOMAIN_PACKAGE_NAME" android:value="com.example" /> //实体类包名
.
.
</application>
步骤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);



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值