Step 1. Add the JitPack repository to your build file
Add it in your root settings.gradle at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
Add it in your settings.gradle.kts at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url = uri("https://jitpack.io") }
}
}
Add to pom.xml
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Add it in your build.sbt at the end of resolvers:
resolvers += "jitpack" at "https://jitpack.io"
Add it in your project.clj at the end of repositories:
:repositories [["jitpack" "https://jitpack.io"]]
Step 2. Add the dependency
dependencies {
implementation 'com.github.jandrad:sugar:1.6.1'
}
dependencies {
implementation("com.github.jandrad:sugar:1.6.1")
}
<dependency>
<groupId>com.github.jandrad</groupId>
<artifactId>sugar</artifactId>
<version>1.6.1</version>
</dependency>
libraryDependencies += "com.github.jandrad" % "sugar" % "1.6.1"
:dependencies [[com.github.jandrad/sugar "1.6.1"]]
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.
Sugar ORM was built in contrast to other ORM's to have:
There are four ways to install Sugar:
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
.
Declare the dependency in Maven:
<dependency>
<groupId>com.github.satyan</groupId>
<artifactId>sugar</artifactId>
<version>1.5</version>
</dependency>
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.
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.
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')
}
===================
After installing, check out how to set up your first database and models here Outdated. Check examples of 1.4 and master below:
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 { ... }
Book book = new Book("isbn123", "Title here", "2nd edition")
book.save();
or
SugarRecord.save(book); // if using the @Table annotation
Book book = Book.findById(Book.class, 1);
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.
Book book = Book.findById(Book.class, 1);
book.delete();
or
SugarRecord.delete(book); // if using the @Table annotation
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
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);
# Ensures entities remain un-obfuscated so table and columns are named correctly
-keep class com.yourpackage.yourapp.domainclasspackage.** { *; }
Please fork this repository and contribute back using pull requests. Features can be requested using issues. All code, comments, and critiques are greatly appreciated.