If you have been working with the Android SQLite database for some time now, you would have noticed something: Setting it up requires a considerable amount of time and loads and loads of boilerplate code.
I recently discovered Sugar ORM, an awesome library that makes it super simple and less time consuming to work with the SQLite database.
In case you didnt already know, an ORM (Object-Relational Mapper) allows you to represent your data as objects and then persist those objects in a relational database, automatically handling the conversion of data for you.
To install Sugar ORM via gradle, simply add this line to your module level gradle dependencies and sync your project:
compile 'com.github.satyan:sugar:1.5'
Enter fullscreen mode Exit fullscreen mode
The current version at the time of this writing is version 1.5.
Sugar ORM requires minimal configuration of your Manifest file. You have to specify SugarApp as your application class by changing the android name attribute in your application tag. You also need to include some meta data about your database such as your database name, version, query log and domain package name. Your manifest file should look something like this:
<application
android:name="com.orm.SugarApp"
android:allowBackup="true"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<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.kwabenaberko" />
</application>
Enter fullscreen mode Exit fullscreen mode
When using the sugar ORM, all data models or classes that you want to persist need to extend SugarRecord and also have at least an empty default constructor like so:
import com.orm.SugarRecord;
public class Developer extends SugarRecord{
private String firstname;
private String lastname;
private String favouriteLanguage;
public Developer(){
}
public Developer(String firstname, String lastname, String favouriteLanguage){
this.firstname = firstname;
this.lastname = lastname;
this.favouriteLanguage = favouriteLanguage;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getFavouriteLanguage() {
return favouriteLanguage;
}
public void setFavouriteLanguage(String favouriteLanguage) {
this.favouriteLanguage = favouriteLanguage;
}
}
Enter fullscreen mode Exit fullscreen mode
Once everything is set up, we can begin performing operations on our model.
Inserting a new record
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Developer developer = new Developer("Kwabena", "Berko", "JavaScript");
developer.save();
}
}
Enter fullscreen mode Exit fullscreen mode
When inserting a new record, Sugar ORM automatically adds an auto-increment id field for you.
Selecting an existing record by its id
Developer developer = Developer.findById(Developer.class, 1); // 1 is the record's id.
Log.v("RESULTS", developer.getId() +
", " + developer.getFirstname() +
", " + developer.getLastname() +
", " + developer.getFavouriteLanguage()
);
Enter fullscreen mode Exit fullscreen mode
Updating an existing record
Developer developer = Developer.findById(Developer.class, 1);
developer.setFavouriteLanguage("Golang");
developer.save();
Enter fullscreen mode Exit fullscreen mode
Selecting all records
List<Developer> developers = Developer.listAll(Developer.class);
for(Developer developer : developers){
Log.v("RESULTS", developer.getId() +
", " + developer.getFirstname() +
", " + developer.getLastname() +
", " + developer.getFavouriteLanguage()
);
}
Enter fullscreen mode Exit fullscreen mode
Deleting an existing record
Developer developer = Developer.findById(Developer.class, 1);
developer.delete();
Enter fullscreen mode Exit fullscreen mode
Happy Coding!
原文链接:Sugar ORM: An easier way to work with SQLite Databases in Android.
暂无评论内容