Insanely easy way to work with Android databases. This is custom version based from master version of Sugar ORM. 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.
Official documentation can be found here - Check some examples below. The example application is provided in the example folder in the source.
- Use SugarRecord.insertOrUpdate( ) to insert or update the data
- Added @IgnoreUpdate annotation
- Automatic one to many insert and query
- Find All with return List
- Drop table
- Encrypted database
See below for more example and explaination ...
Add on your app build.gradle
dependencies {
compile 'com.sylversky.library:sugarorm:1.0.1'
}
Extends SugarApp on your application class
public class ClientApp extends SugarApp {
}
or you can manualy init the sugar orm on you application class
public class ClientApp extends Application {
@Override
public void onCreate() {
super.onCreate();
SugarContext.init(this);
}
@Override
public void onTerminate() {
super.onTerminate();
SugarContext.terminate();
}
}
Open your AndroidManifest.xml and put meta data configuration inside appication tag:
<application ...
android:name=".ClientApp">
.
.
<meta-data android:name="DATABASE" android:value="sugar_example.db" /> //database file name
<meta-data android:name="VERSION" android:value="1" /> //database version
<meta-data android:name="QUERY_LOG" android:value="true" /> //database log
<meta-data android:name="DOMAIN_PACKAGE_NAME" android:value="yourentitypackage" /> //location of package for all of your entity table
.
.
</application>
@Table
public class Person {
Long id;
String regId;
String name;
Date dob;
public Person(){}
public Person(Long id, String regId, String name, Date dob){
this.id = id;
this.regId = regId;
this.name = name;
this.dob = dob;
}
}
This is will create table PERSON with columns ID,REG_ID,NAME,DOB on SQLite. *** Field "Long id" is a must to declare. This will be the primary key (autoincrement) for your table. *** When you need to create your own constructor, you must add an empty constructor
The default primary key is with Long data type. If you need other data type (for example String), just add new filed and put the @Unique annotation but keep the "Long id".
@Table
public class Person {
Long id;
@Unique
String personId;
String regId;
String name;
Date dob;
public Person(){}
public Person(Long id, String personId, String regId, String name, Date dob){
this.id = id;
this.personId = personId;
this.regId = regId;
this.name = name;
this.dob = dob;
}
}
Person person = new Person(1l,"abc12345","Leonardo", new Date());
SugarRecord.insertOrUpdate(person);
Person person = SugarRecord.findById(Person.class, 1);
List<Person> person = SugarRecord.find(Person.class, "NAME = ?", "Leonardo");
List<Person> person = SugarRecord.findAll(Person.class);
Person person = SugarRecord.findById(Person.class, 1);
person.regId = "def12345"
person.name = "Donatelo"; // modify the values
SugarRecord.update(person);
Person person = SugarRecord.findById(Person.class, 1);
SugarRecord.delete(person);
List<Person> persons = new ArrayList<>();
persons.add(new Person(1l,"abc12345","Leonardo", new Date()));
persons.add(new Person(2l,"def12345","Donatelo", new Date()));
persons.add(new Person(3l,"ghi12345","Michaelangelo", new Date()));
persons.add(new Person(4l,"jkl12345","Raphael", new Date()));
SugarRecord.insertOrUpdate(persons);
SugarRecord.drop(Person.class);
*** Becarefull when use drop. This function will remove the structure of table.
You can ignoring update for specified column. Only work when use SugarRecord.insertOrUpdate( ) function. For example : need to give flag data read/unread, but ignoring to replace when refreshed from api server. But, you can update specified column with SugarRecord.update( ) function.
@Table
public class Person {
Long id;
String regId;
String name;
Date dob;
@IgnoreUpdate
boolean read;
public Person(){}
public Person(Long id, String regId, String name, Date dob){
this.id = id;
this.regId = regId;
this.name = name;
this.dob = dob;
}
}
Person person = new Person(1l,"abc12345","Leonardo", new Date());
SugarRecord.insertOrUpdate(person);
Now read flag is 'false';
Person person = SugarRecord.findById(Person.class, 1);
person.read = true;
SugarRecord.update(person);
Now read flag is 'true';
Person person = new Person(1l,"abc12345","Leonardo", new Date());
SugarRecord.insertOrUpdate(person);
This will insert data person with default read value is 'false', but because use @IgnoreUpdate now read flag still 'true'
When you need other entity be member of your entity, Sugar will automatically manage it.
@Table
public class Address {
Long id;
String street;
String province;
public Address(){}
public Address(Long id, String street, String province){
this.id = id;
this.street = street;
this.province = province;
}
}
@Table
public class Person {
Long id;
String regId;
String name;
Date dob;
List<Address> address;
public Person(){}
public Person(Long id, String regId, String name, Date dob, List<Address> address){
this.id = id;
this.regId = regId;
this.name = name;
this.dob = dob;
this.address = address;
}
}
List<Address> addressList = new ArrayList();
addressList.add(new Address(1l,"my office","my province"));
addressList.add(new Address(2l,"my home","my countryside province"));
Person person = new Person(1l,"abc12345","Leonardo", new Date(), addressList);
SugarRecord.insertOrUpdate(person);
Person and address will automatically insert into different table.
Person person = SugarRecord.findById(Person.class, 1);
List<Address> addresList = person.address;
With just one find( ) function, person and address automatically combined.
- Just declare your new column inside your entity.
- Increase meta VERSION value on AndroidManifest.xml.
<application ... android:name=".ClientApp"> . . <meta-data android:name="VERSION" android:value="2" /> //database version . . </application>
Database schema will automatically updated.
Just add SugarDbConfiguration with your password for encrypt when initialize Sugar
public class ClientApp extends Application {
@Override
public void onCreate() {
super.onCreate();
SugarContext.init(this, new SugarDbConfiguration().setEncryptedPassword("mysugarpassword"));
}
@Override
public void onTerminate() {
SugarContext.terminate();
super.onTerminate();
}
}
# Ensures entities remain un-obfuscated so table and columns are named correctly
-keep class com.yourpackage.yourapp.domainclasspackage.** { *; }
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..." )
Please fork this repository and contribute back using pull requests. Features can be requested using issues. All code, comments, and critiques are greatly appreciated.