1
What are the advantages and disadvantages of using SugarRecord
when compared to native functions SQLite
,
Example when creating a table with Sqlite Native Functions
DatabaseHelper.java
public class DatabaseHelper extends SQLiteOpenHelper {
// Logcat tag
private static final String LOG = "DatabaseHelper";
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "contactsManager";
// Table Names
private static final String TABLE_TODO = "todos";
private static final String TABLE_TAG = "tags";
private static final String TABLE_TODO_TAG = "todo_tags";
// Common column names
private static final String KEY_ID = "id";
private static final String KEY_CREATED_AT = "created_at";
// NOTES Table - column nmaes
private static final String KEY_TODO = "todo";
private static final String KEY_STATUS = "status";
// TAGS Table - column names
private static final String KEY_TAG_NAME = "tag_name";
// NOTE_TAGS Table - column names
private static final String KEY_TODO_ID = "todo_id";
private static final String KEY_TAG_ID = "tag_id";
// Table Create Statements
// Todo table create statement
private static final String CREATE_TABLE_TODO = "CREATE TABLE "
+ TABLE_TODO + "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_TODO
+ " TEXT," + KEY_STATUS + " INTEGER," + KEY_CREATED_AT
+ " DATETIME" + ")";
// Tag table create statement
private static final String CREATE_TABLE_TAG = "CREATE TABLE " + TABLE_TAG
+ "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_TAG_NAME + " TEXT,"
+ KEY_CREATED_AT + " DATETIME" + ")";
// todo_tag table create statement
private static final String CREATE_TABLE_TODO_TAG = "CREATE TABLE "
+ TABLE_TODO_TAG + "(" + KEY_ID + " INTEGER PRIMARY KEY,"
+ KEY_TODO_ID + " INTEGER," + KEY_TAG_ID + " INTEGER,"
+ KEY_CREATED_AT + " DATETIME" + ")";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// creating required tables
db.execSQL(CREATE_TABLE_TODO);
db.execSQL(CREATE_TABLE_TAG);
db.execSQL(CREATE_TABLE_TODO_TAG);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// on upgrade drop older tables
db.execSQL("DROP TABLE IF EXISTS " + TABLE_TODO);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_TAG);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_TODO_TAG);
// create new tables
onCreate(db);
}
And with the Sugar Sugarrecord
public class Book extends SugarRecord<Book> {
String title;
String edition;
public Book(){
}
public Book(String title, String edition){
this.title = title;
this.edition = edition;
}
}
This is a kind of question whose answer is subjective. Anyway, the perks you can enter the Sugarrecord website itself and the disadvantages is simply to become dependent on a third party lib when you could make that service yourself.
– Androiderson