Convert Figma logo to code with AI

chennaione logosugar

Insanely easy way to work with Android Database.

2,621
583
2,621
302

Top Related Projects

2,620

Insanely easy way to work with Android Database.

Realm is a mobile database: a replacement for SQLite & ORMs

12,626

greenDAO is a light & fast ORM solution for Android that maps objects to SQLite databases.

Android Database - first and fast, lightweight on-device vector database

4,872

A blazing fast, powerful, and very simple ORM android database library that writes database code for you.

3,139

requery - modern SQL based query & persistence for Java / Kotlin / Android

Quick Overview

Sugar ORM is a lightweight and simple ORM (Object-Relational Mapping) library for Android. It simplifies database operations by allowing developers to work with Java objects instead of writing complex SQL queries, making it easier to persist and retrieve data in Android applications.

Pros

  • Easy to set up and use, with minimal configuration required
  • Provides a clean and intuitive API for database operations
  • Supports automatic table creation and schema updates
  • Offers good performance for basic CRUD operations

Cons

  • Limited support for complex queries and relationships
  • Lack of advanced features found in more robust ORM solutions
  • Not actively maintained, with the last update being several years ago
  • May not be suitable for large-scale or complex database structures

Code Examples

  1. Defining a model:
@Table
public class Book extends SugarRecord {
    String title;
    String author;
    int year;

    public Book() {}

    public Book(String title, String author, int year) {
        this.title = title;
        this.author = author;
        this.year = year;
    }
}
  1. Saving an object:
Book book = new Book("1984", "George Orwell", 1949);
book.save();
  1. Querying objects:
List<Book> books = Book.listAll(Book.class);
Book book = Book.findById(Book.class, 1);
List<Book> orwellBooks = Book.find(Book.class, "author = ?", "George Orwell");
  1. Deleting an object:
Book book = Book.findById(Book.class, 1);
book.delete();

Getting Started

  1. Add the Sugar ORM dependency to your build.gradle file:
dependencies {
    implementation 'com.github.satyan:sugar:1.5'
}
  1. Configure your AndroidManifest.xml:
<application
    android:name="com.orm.SugarApp"
    ...>
    <meta-data android:name="DATABASE" android:value="sugar_example.db" />
    <meta-data android:name="VERSION" android:value="1" />
    <meta-data android:name="QUERY_LOG" android:value="true" />
    <meta-data android:name="DOMAIN_PACKAGE_NAME" android:value="com.example" />
</application>
  1. Create your model classes extending SugarRecord and start using Sugar ORM in your Android application.

Competitor Comparisons

2,620

Insanely easy way to work with Android Database.

Pros of Sugar

  • More active development with recent commits and releases
  • Larger community with more stars, forks, and contributors
  • Comprehensive documentation and examples available

Cons of Sugar

  • Potentially more complex due to additional features
  • May have a steeper learning curve for beginners
  • Larger codebase could lead to increased overhead

Code Comparison

Sugar:

@Table("users")
public class User extends SugarRecord<User> {
    @Column(name = "name")
    String name;
    @Column(name = "email")
    String email;
}

Sugar>:

@Table("users")
public class User extends SugarRecord {
    String name;
    String email;
}

Summary

Sugar appears to be a more mature and feature-rich ORM library for Android, with a larger community and more active development. It offers more advanced features and annotations for database mapping. Sugar>, on the other hand, seems to have a simpler API and potentially easier setup, which might be beneficial for smaller projects or developers new to ORM concepts. The code comparison shows that Sugar uses more explicit annotations for column mapping, while Sugar> relies on convention over configuration. Ultimately, the choice between the two depends on project requirements and developer preferences.

Realm is a mobile database: a replacement for SQLite & ORMs

Pros of Realm Java

  • Better performance and efficiency, especially for large datasets
  • Supports real-time synchronization and offline-first capabilities
  • More advanced querying capabilities and support for complex data models

Cons of Realm Java

  • Steeper learning curve and more complex setup process
  • Less flexibility in terms of database schema changes
  • Larger library size and potential impact on app size

Code Comparison

Sugar:

Book book = new Book(context);
book.setTitle("Realm vs Sugar");
book.setAuthor("Developer");
book.save();

Realm Java:

realm.executeTransaction(r -> {
    Book book = r.createObject(Book.class);
    book.setTitle("Realm vs Sugar");
    book.setAuthor("Developer");
});

Summary

While Sugar offers simplicity and ease of use, Realm Java provides better performance and more advanced features. Sugar is ideal for smaller projects with simple data models, while Realm Java is better suited for larger, more complex applications that require real-time synchronization and efficient handling of large datasets. The choice between the two depends on the specific needs of your project and your familiarity with each library's ecosystem.

12,626

greenDAO is a light & fast ORM solution for Android that maps objects to SQLite databases.

Pros of greenDAO

  • Better performance due to compile-time code generation
  • More advanced querying capabilities and support for complex relationships
  • Active development and regular updates

Cons of greenDAO

  • Steeper learning curve and more complex setup
  • Requires additional build-time processing

Code Comparison

greenDAO:

@Entity
public class User {
    @Id private Long id;
    private String name;
    private int age;
}

Sugar:

public class User extends SugarRecord {
    String name;
    int age;
}

Summary

greenDAO offers better performance and more advanced features, but comes with a steeper learning curve. Sugar provides a simpler API and easier setup, but may lack some advanced features and optimizations.

greenDAO is more suitable for complex projects with performance requirements, while Sugar is ideal for simpler applications where ease of use is prioritized over advanced functionality.

Both libraries aim to simplify database operations in Android development, but they take different approaches to achieve this goal. The choice between them depends on the specific needs of the project and the developer's preferences.

Android Database - first and fast, lightweight on-device vector database

Pros of ObjectBox

  • Higher performance and efficiency, especially for large datasets
  • Support for relations and queries with automatic indexing
  • Built-in encryption and multi-platform support (Android, iOS, Linux, macOS, Windows)

Cons of ObjectBox

  • Steeper learning curve due to more complex API
  • Requires additional setup and configuration
  • Less straightforward for simple use cases compared to Sugar ORM

Code Comparison

Sugar ORM:

@Table
public class Book extends SugarRecord<Book> {
    String title;
    String author;
    public Book(String title, String author) {
        this.title = title;
        this.author = author;
    }
}

ObjectBox:

@Entity
public class Book {
    @Id long id;
    String title;
    String author;
    public Book(String title, String author) {
        this.title = title;
        this.author = author;
    }
}

Both ORMs use annotations to define entities, but ObjectBox requires an explicit @Id field. Sugar ORM extends SugarRecord for built-in CRUD operations, while ObjectBox uses a separate Box<T> interface for database operations. ObjectBox offers more advanced features and better performance, but Sugar ORM provides a simpler API for basic use cases.

4,872

A blazing fast, powerful, and very simple ORM android database library that writes database code for you.

Pros of DBFlow

  • More powerful and flexible ORM with advanced querying capabilities
  • Better performance due to compile-time annotation processing
  • Supports complex relationships and migrations

Cons of DBFlow

  • Steeper learning curve due to more complex API
  • Requires more boilerplate code compared to Sugar's simpler approach
  • Heavier dependency with larger library size

Code Comparison

Sugar:

@Table
public class Book extends SugarRecord {
    String title;
    String author;
}

Book book = new Book();
book.title = "1984";
book.author = "George Orwell";
book.save();

DBFlow:

@Table(database = AppDatabase.class)
public class Book extends BaseModel {
    @PrimaryKey(autoincrement = true)
    long id;
    @Column
    String title;
    @Column
    String author;
}

Book book = new Book();
book.setTitle("1984");
book.setAuthor("George Orwell");
book.save();

DBFlow requires more setup and annotations, but offers greater control and flexibility. Sugar provides a simpler API with less configuration, making it easier for beginners but potentially limiting for complex scenarios.

3,139

requery - modern SQL based query & persistence for Java / Kotlin / Android

Pros of Requery

  • More comprehensive ORM with support for multiple databases (SQLite, MySQL, PostgreSQL)
  • Offers reactive extensions and RxJava integration
  • Provides a more flexible and powerful query API

Cons of Requery

  • Steeper learning curve due to more complex API
  • Requires more setup and configuration compared to Sugar ORM
  • May be overkill for simple database operations

Code Comparison

Sugar ORM:

Book book = new Book(context, "Title", "Author");
book.save();
List<Book> books = Select.from(Book.class).list();

Requery:

Book book = new Book();
book.setTitle("Title");
book.setAuthor("Author");
data.insert(book);
Result<Book> books = data.select(Book.class).get();

Summary

Requery offers a more powerful and flexible ORM solution with support for multiple databases and reactive programming. However, it comes with a steeper learning curve and more complex setup. Sugar ORM, on the other hand, provides a simpler and more straightforward approach to database operations, making it easier to use for basic tasks but potentially limiting for more complex scenarios.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Sugar ORM Build Status Coverage Status Code Triagers Badge

Join the chat at https://gitter.im/satyan/sugar

Insanely easy way to work with Android databases.

Official documentation can be found here - Check some examples below. The example application is provided in the example folder in the source.

Looking for contributors

We need contributors to help maintain this project, ask @satyan for repo permission

Otherwise you can use another ORM, like https://github.com/requery/requery or https://realm.io/

Features

Sugar ORM was built in contrast to other ORM's to have:

  • A simple, concise, and clean integration process with minimal configuration.
  • Automatic table and column naming through reflection.
  • Support for migrations between different schema versions.

Installing

There are four ways to install Sugar:

As a Gradle dependency

This is the preferred way. Simply add:

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

to your project dependencies and run gradle build or gradle assemble.

As a Maven dependency

Declare the dependency in Maven:

<dependency>
    <groupId>com.github.satyan</groupId>
    <artifactId>sugar</artifactId>
    <version>1.5</version>
</dependency>

As a library project

Download the source code and import it as a library project in Eclipse. The project is available in the folder library. For more information on how to do this, read here.

As a jar

Visit the releases page to download jars directly. You can drop them into your libs folder and configure the Java build path to include the library. See this tutorial for an excellent guide on how to do this.

How to use master version

First, download sugar repository

git clone git@github.com:satyan/sugar.git

include this in your settings.gradle

include ':app' // your module app
include ':sugar'

def getLocalProperty(prop) {
	Properties properties = new Properties()
	properties.load(new File(rootDir.absolutePath + '/local.properties').newDataInputStream())
	return properties.getProperty(prop, '')
}

project(':sugar').projectDir = new File(getLocalProperty('sugar.dir'))

include this in your local.properties

sugar.dir=/path/to/sugar/library

add sugar project to the dependencies of your main project (build.gradle)

dependencies {
    compile project(':sugar')
}

You should also comment this line just comment this line (library/build.gradle): https://github.com/satyan/sugar/blob/master/library%2Fbuild.gradle#L2

// apply from: '../maven_push.gradle'

===================

After installing, check out how to set up your first database and models here Outdated. Check examples of 1.4 and master below:

Examples

SugarRecord

public class Book extends SugarRecord {
  @Unique
  String isbn;
  String title;
  String edition;

  // Default constructor is necessary for SugarRecord
  public Book() {

  }

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

or

@Table
public class Book { ... }

Save Entity

Book book = new Book("isbn123", "Title here", "2nd edition")
book.save();

or

SugarRecord.save(book); // if using the @Table annotation 

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();

or

SugarRecord.delete(book); // if using the @Table annotation 

Update Entity based on Unique values

Book book = new Book("isbn123", "Title here", "2nd edition")
book.save();

// Update book with isbn123
Book sameBook = new Book("isbn123", "New Title", "5th edition")
sameBook.update();

book.getId() == sameBook.getId(); // true

or

SugarRecord.update(sameBook); // if using the @Table annotation 

Bulk Insert

List<Book> books = new ArrayList<>();
books.add(new Book("isbn123", "Title here", "2nd edition"))
books.add(new Book("isbn456", "Title here 2", "3nd edition"))
books.add(new Book("isbn789", "Title here 3", "4nd edition"))
SugarRecord.saveInTx(books);

When using ProGuard

# Ensures entities remain un-obfuscated so table and columns are named correctly
-keep class com.yourpackage.yourapp.domainclasspackage.** { *; }

Known Issues.

1. Instant Run.

Instant-Run seems to prevent Sugar ORM from finding the "table" classes, therefore it cannot create the DB tables if you run the app for the first time

When running your app for the first time Turn off Instant run once to allow for the DB tables to be created You can enable it after the tables have been created.

To disable Instant-Run in Android Studio:

(Preferences (Mac) or Settings (PC) -> Build, Execution, Deployment -> Instant Run -> Untick "Enable Instant Run..." )

CHANGELOG

Contributing

Please fork this repository and contribute back using pull requests. Features can be requested using issues. All code, comments, and critiques are greatly appreciated.