Working With Database - Artlite/SQDatabase GitHub Wiki
- Implement the
SQModelinterface for your object;
- Add the
@SQFieldannotation for field that you need to save into database.
WARNING: Every SQModel object have ID as Integer (auto increment) and you need to return it from method of SQModel interface
public interface SQModel extends BaseColumns {
/**
* Method which provide the getting of the ID fro {@link SQModel}
*
* @return id for {@link SQModel}
*/
int id();
}-
Double; -
Float; -
Integer; -
Boolean; -
String; -
Date; -
Blobwhich create fromParcelableobject ONLY; -
SQMap; -
Bitmap; -
SQBitmap; -
SQBooleanList; -
SQDoubleList; -
SQFloatList; -
SQIntegerList; -
SQLongList; -
SQStringList; -
SQParcelableList.
Create the object and add SQField annotation for field that you need to save into database
/**
* User object
*/
public class User implements SQModel {
protected static final String K_TABLE_NAME = User.class.getSimpleName();
private int id;
@SQField
private String name;
@SQField
private String surname;
@SQField
private String aboutMe;
@SQField
private Date creation = new Date();
@SQField
private boolean favorite = false;
@SQField
private SQBitmap avatar;
@SQField
private Address address;
//Lists
@SQField
private SQBooleanList booleans;
@SQField
private SQDoubleList doubles;
@SQField
private SQFloatList floats;
@SQField
private SQIntegerList integers;
@SQField
private SQLongList longs;
@SQField
private SQStringList strings;
@SQField
private AddressList addresses;
/**
* Constructor which provide the create {@link User} from
*
* @param name user name
* @param surname user surname
* @param aboutMe user information
*/
public User(@Nullable final String name,
@Nullable final String surname,
@Nullable final String aboutMe,
@Nullable final Bitmap avatar) {
this.name = name;
this.surname = surname;
this.aboutMe = aboutMe;
this.avatar = new SQBitmap(avatar, K_MAX_WIDTH, K_MAX_HEIGHT);
this.address = new Address();
//List functional
this.addresses = new AddressList();
this.longs = new SQLongList();
this.booleans = new SQBooleanList();
this.doubles = new SQDoubleList();
this.floats = new SQFloatList();
this.integers = new SQIntegerList();
this.strings = new SQStringList();
//Fill lists
this.addresses.addAll(Arrays.asList(new Address(), new Address()));
this.longs.addAll(Arrays.asList(new Long(10), new Long(20), new Long(30)));
this.booleans.addAll(Arrays.asList(true, false, false));
this.doubles.addAll(Arrays.asList(12.3, 12.5, 15.0));
this.floats.addAll(Arrays.asList(new Float(12.3), new Float(12.5), new Float(15.0)));
this.integers.addAll(Arrays.asList(12, 13, 15));
this.strings.addAll(Arrays.asList(SQRandomHelper.generateString(10),
SQRandomHelper.generateString(10),
SQRandomHelper.generateString(10)));
}
/**
* Method which provide the creating {@link User} from {@link Cursor}
*
* @param cursor instance of {@link Cursor}
*/
public User(@NonNull final Cursor cursor) {
apply(cursor);
}
/**
* Method which provide the getting of the ID fro {@link SQModel}
*
* @return id for {@link SQModel}
*/
@Override
public int id() {
return id;
}
/**
* Method which provide the getting of the SQL table name
*
* @return table name
*/
@Nullable
@Override
public String table() {
return K_TABLE_NAME;
}
/**
* Method which provide the applying data from {@link Cursor}
*
* @param cursor instance of {@link Cursor}
*/
@Override
public void apply(@Nullable Cursor cursor) {
this.id = SQModelHelper.getID(cursor);
this.name = SQModelHelper.getString(cursor, "name");
this.surname = SQModelHelper.getString(cursor, "surname");
this.aboutMe = SQModelHelper.getString(cursor, "aboutMe");
this.creation = SQModelHelper.getDate(cursor, "creation");
this.address = SQModelHelper.getObject(cursor, Address.CREATOR, "address");
this.avatar = SQModelHelper.getObject(cursor, SQBitmap.CREATOR, "avatar");
//Get lists
this.addresses = SQModelHelper.getList(cursor, AddressList.CREATOR, "addresses");
this.booleans = SQModelHelper.getList(cursor, SQListType.BOOLEAN, "booleans");
this.doubles = SQModelHelper.getList(cursor, SQListType.DOUBLE, "doubles");
this.floats = SQModelHelper.getList(cursor, SQListType.FLOAT, "floats");
this.integers = SQModelHelper.getList(cursor, SQListType.INTEGER, "integers");
this.strings = SQModelHelper.getList(cursor, SQListType.STRING, "strings");
setFavorite(SQModelHelper.getBoolean(cursor, "favorite"));
}
}For saving object you need to call SQDatabase.insert(object), example:
final User user = new User();
SQDatabase.insert(user);For updating object you need to call SQDatabase.update(object). This can be applicable for object that was get from SQDatabase and have the ID. Example:
final User user = //User that was got from database and have the ID as auto increment integer
SQDatabase.update(user);For deleting object you need to call SQDatabase.delete(object). This can be applicable for object that was get from SQDatabase and have the ID. Example:
final User user = //User that was got from database and have the ID as auto increment integer
SQDatabase.delete(user);For getting object from database you need to call SQDatabase.select(...). WARNING: Every performing with database executing on main thread, you should call it from background thread. Example:
SQDatabase.select(User.class, new SQCursorCallback<User>() {
@Nullable
@Override
public User convert(@NonNull Cursor cursor) {
return new User(cursor);
}
});