All Products
Search
Document Center

Database storage

Last Updated: May 25, 2021

The database storage provided by mPaaS is based on the OrmLite architecture and provides database encryption capabilities at the underlying layer. You can insert, delete, change, or query data by calling the API operations provided by com.j256.ormlite.dao.Dao.

Note: Do not directly encrypt data in databases. If you do, a crash occurs during data decryption at the native layer. To encrypt data, we recommend that you create a database for storing the data and then copy the data from its original database to the newly created database.

Example

Generate a Database table

 
  1. // The name of the Database table. By default, the table name is the same as the class name.
  2. @DatabaseTable
  3. public class User {
  4. // The primary key.
  5. @DatabaseField(generatedId = true)
  6. public int id;
  7. // The value of the name field must be unique.
  8. @DatabaseField(unique = true)
  9. public String name;
  10. @DatabaseField
  11. public int color;
  12. @DatabaseField
  13. public long timestamp;
  14. }

Create an OrmLiteSqliteOpenHelper class

Create a custom DemoOrmLiteSqliteOpenHelper class that inherits the OrmLiteSqliteOpenHelper class. You can create a database and encrypt the data in the database by DemoOrmLiteSqliteOpenHelper.

 
  1. public class DemoOrmLiteSqliteOpenHelper extends OrmLiteSqliteOpenHelper {
  2. /**
  3. * The database name.
  4. */
  5. private static final String DB_NAME = "com_mpaas_demo_storage.db";
  6. /**
  7. * The version of the current database.
  8. */
  9. private static final int DB_VERSION = 1;
  10. /**
  11. * Enter an encryption key. mPaaS supports database encryption to ensure data security on devices. If the encryption key is null, data is not encrypted.
  12. * Note: You can set the password only once. No API operation is provided for changing the password. You cannot set a password for an unencrypted database. If you do, a crash occurs.
  13. */
  14. private static final String DB_PASSWORD = "mpaas";
  15. public DemoOrmLiteSqliteOpenHelper(Context context) {
  16. super(context, DB_NAME, null, DB_VERSION);
  17. setPassword(DB_PASSWORD);
  18. }
  19. /**
  20. * The callback function used when a database is created.
  21. *
  22. * @param sqLiteDatabase The database.
  23. * @param connectionSource Connect to the database.
  24. */
  25. @Override
  26. public void onCreate(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource) {
  27. try {
  28. // Create a table named User.
  29. TableUtils.createTableIfNotExists(connectionSource, User.class);
  30. } catch (SQLException e) {
  31. e.printStackTrace();
  32. }
  33. }
  34. /**
  35. * The callback function used when data in a database is changed.
  36. *
  37. * @param database The database.
  38. * @param connectionSource Connect to the database.
  39. * @param oldVersion The old version of the database.
  40. * @param newVersion The new version of the database.
  41. */
  42. @Override
  43. public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {
  44. try {
  45. // Delete the table named User in the old version of the database. Ignore the error.
  46. TableUtils.dropTable(connectionSource, User.class, true);
  47. } catch (SQLException e) {
  48. e.printStackTrace();
  49. }
  50. try {
  51. // Create another table named User.
  52. TableUtils.createTableIfNotExists(connectionSource, User.class);
  53. } catch (SQLException e) {
  54. e.printStackTrace();
  55. }
  56. }
  57. }

Query data

In the following example, all the data in the User table is queried and sorted by the timestamp field in ascending order.

 
  1. /**
  2. * Initialize the data in the database.
  3. */
  4. private void initData() {
  5. mData.clear();
  6. try {
  7. mData.addAll(mDbHelper.getDao(User.class).queryBuilder().orderBy("timestamp", true).query());
  8. } catch (SQLException e) {
  9. e.printStackTrace();
  10. }
  11. }

Insert data

 
  1. /**
  2. * Insert user information.
  3. *
  4. * @param user User information.
  5. */
  6. private void insertUser(User user) {
  7. if (null == user) {
  8. return;
  9. }
  10. try {
  11. // mDbHelper = new DemoOrmLiteSqliteOpenHelper(this); For more information, see the "Create an OrmLiteSqliteOpenHelper" section.
  12. mDbHelper.getDao(User.class).create(user);
  13. } catch (SQLException e) {
  14. e.printStackTrace();
  15. }
  16. }

Delete data

 
  1. /**
  2. * Delete user information.
  3. *
  4. * @param user User information.
  5. */
  6. private void deleteUser(User user) {
  7. try {
  8. // mDbHelper = new DemoOrmLiteSqliteOpenHelper(this); For more information, see the "Create an OrmLiteSqliteOpenHelper" section.
  9. mDbHelper.getDao(User.class).delete(user);
  10. } catch (SQLException e) {
  11. e.printStackTrace();
  12. }
  13. }