The mPaaS database storage feature is based on the OrmLite framework and provides underlying database encryption. You can use the following interfaces to create, delete, update, and query data:
For baseline 10.2.3 and later:
com.alibaba.j256.ormlite.dao.DaoFor baseline 10.1.68 and earlier:
com.j256.ormlite.dao.Dao
Do not directly encrypt an existing database. This will cause a crash in the native layer during decryption. Instead, create a new encrypted database and copy the content from the original database into it.
Usage examples
Generate a data table
// The database table name. The class name is used by default.
@DatabaseTable
public class User {
// Primary key
@DatabaseField(generatedId = true)
public int id;
// The name field must be unique.
@DatabaseField(unique = true)
public String name;
@DatabaseField
public int color;
@DatabaseField
public long timestamp;
}Create an OrmLiteSqliteOpenHelper
You can create a custom DemoOrmLiteSqliteOpenHelper class that inherits from OrmLiteSqliteOpenHelper and use it to create and encrypt a database.
For baseline 10.2.3 and later:
public class DemoOrmLiteSqliteOpenHelper extends OrmLiteSqliteOpenHelper { /** * Database name */ private static final String DB_NAME = "com_mpaas_demo_storage.db"; /** * Current database version */ private static final int DB_VERSION = 1; /** * The database encryption key. mPaaS supports database encryption to make data on the device more secure. If this is null, the database is not encrypted. * Note: The password can only be set once. No API is provided to change the password. You cannot set a password for an unencrypted database to encrypt it. This will cause a crash. */ private static final String DB_PASSWORD = "mpaas"; public DemoOrmLiteSqliteOpenHelper(Context context) { super(context, DB_NAME, null, DB_VERSION); setPassword(DB_PASSWORD); } /** * The callback function for when the database is created. * * @param sqLiteDatabase The database * @param connectionSource The connection */ @Override public void onCreate(MPSQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource) { try { // Create the User table TableUtils.createTableIfNotExists(connectionSource, User.class); } catch (SQLException e) { e.printStackTrace(); } } /** * The callback function for when the database is updated. * * @param database The database * @param connectionSource The connection * @param oldVersion The old database version * @param newVersion The new database version */ @Override public void onUpgrade(MPSQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) { try { // Delete the old User table and ignore errors TableUtils.dropTable(connectionSource, User.class, true); } catch (SQLException e) { e.printStackTrace(); } try { // Recreate the User table TableUtils.createTableIfNotExists(connectionSource, User.class); } catch (SQLException e) { e.printStackTrace(); } } }For baseline 10.1.68 and earlier:
public class DemoOrmLiteSqliteOpenHelper extends OrmLiteSqliteOpenHelper { /** * Database name */ private static final String DB_NAME = "com_mpaas_demo_storage.db"; /** * Current database version */ private static final int DB_VERSION = 1; /** * The database encryption key. mPaaS supports database encryption to make data on the device more secure. If this is null, the database is not encrypted. * Note: The password can only be set once. No API is provided to change the password. You cannot set a password for an unencrypted database to encrypt it. This will cause a crash. */ private static final String DB_PASSWORD = "mpaas"; public DemoOrmLiteSqliteOpenHelper(Context context) { super(context, DB_NAME, null, DB_VERSION); setPassword(DB_PASSWORD); } /** * The callback function for when the database is created. * * @param sqLiteDatabase The database * @param connectionSource The connection */ @Override public void onCreate(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource) { try { // Create the User table TableUtils.createTableIfNotExists(connectionSource, User.class); } catch (SQLException e) { e.printStackTrace(); } } /** * The callback function for when the database is updated. * * @param database The database * @param connectionSource The connection * @param oldVersion The old database version * @param newVersion The new database version */ @Override public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) { try { // Delete the old User table and ignore errors TableUtils.dropTable(connectionSource, User.class, true); } catch (SQLException e) { e.printStackTrace(); } try { // Recreate the User table TableUtils.createTableIfNotExists(connectionSource, User.class); } catch (SQLException e) { e.printStackTrace(); } } }
Query data
This example queries all data from the User table and sorts it in ascending order by the timestamp field.
/**
* Initialize DB data
*/
private void initData() {
mData.clear();
try {
mData.addAll(mDbHelper.getDao(User.class).queryBuilder().orderBy("timestamp", true).query());
} catch (SQLException e) {
e.printStackTrace();
}
}Insert data
/**
* Insert user information
*
* @param user The user information
*/
private void insertUser(User user) {
if (null == user) {
return;
}
try {
// mDbHelper = new DemoOrmLiteSqliteOpenHelper(this); For more information, see Create an OrmLiteSqliteOpenHelper above.
mDbHelper.getDao(User.class).create(user);
} catch (SQLException e) {
e.printStackTrace();
}
}Delete data
/**
* Delete user information
*
* @param user The user information
*/
private void deleteUser(User user) {
try {
// mDbHelper = new DemoOrmLiteSqliteOpenHelper(this); For more information, see Create an OrmLiteSqliteOpenHelper above.
mDbHelper.getDao(User.class).delete(user);
} catch (SQLException e) {
e.printStackTrace();
}
}