All Products
Search
Document Center

Mobile Platform as a Service:Database storage

Last Updated:Nov 02, 2022

Database storage provided by mPaaS provides the capability of encrypting database underlayer based on OrmLite architecture. You can call the following interface to implement data addition, deletion, modification and query in databases.

  • 10.2.3 and later baselines: com.alibaba.j256.ormlite.dao.Dao

  • 10.1.68 and earlier baseline: com.j256.ormlite.dao.Dao

Note

When using the database, please do not directly encrypt the original database, otherwise it will cause the native layer decryption crash. It is recommended that you create a new encrypted database first, and then copy the contents of the original database to the newly created encrypted database.

Examples

Generate tables

// Database table name, it is the class name by default
@DatabaseTable
public class User {
    // Primary key
    @DatabaseField(generatedId = true)
    public int id;
    // The value of name field must be unique
    @DatabaseField(unique = true)
    public String name;
    @DatabaseField
    public int color;
    @DatabaseField
    public long timestamp;
}

Create OrmLiteSqliteOpenHelper

Customize a DemoOrmLiteSqliteOpenHelper which inherits from OrmLiteSqliteOpenHelper.

With OrmLiteSqliteOpenHelper, a database can be created and encrypted.

  • 10.2.3 and later baselines:

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;

    /**
     * Database encryption key. mPaaS supports encrypting databases to make the data safer on devices. If it is null, the databases will not be encrypted.
     * Note: The password can only be set once, and there is no API for changing the password; encryption of the unencrypted library setting password is not supported (it 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);
    }

    /**
     * Callback function upon database creation
     *
     * @param sqLiteDatabase: Database
     * @param connectionSource: Connection
     */
    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource) {
        try {
            // Create User table
            TableUtils.createTableIfNotExists(connectionSource, User.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * Callback function upon database update
     *
     * @param database: Database
     * @param connectionSource: Connection
     * @param oldVersion: Old database version
     * @param newVersion: New database version
     */
    @Override
    public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {
        try {
            // Delete the old version of the User table, and ignore errors
            TableUtils.dropTable(connectionSource, User.class, true);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            // Rereate User table
            TableUtils.createTableIfNotExists(connectionSource, User.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
  • 10.1.68 and earlier baseline:

Database encryption key. mPaaS supports encrypting databases to make the data safer on devices. If it is null, the databases will not be encrypted. Database encryption key. mPaaS supports encrypting databases to make the data safer on devices. If it is null, the databases will not be encrypted.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;

    /**
     * Database encryption key. mPaaS supports encrypting databases to make the data safer on devices. If it is null, the databases will not be encrypted.
     * Note: The password can only be set once, and there is no API for changing the password; encryption of the unencrypted library setting password is not supported (it 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);
    }

    /**
     * Callback function upon database creation
     *
     * @param sqLiteDatabase   Database
     * @param connectionSource Connection
     */
    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource) {
        try {
            // Create User table
            TableUtils.createTableIfNotExists(connectionSource, User.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * Callback function upon database update
     *
     * @param database         Database
     * @param connectionSource Connection
     * @param oldVersion       Old database version
     * @param newVersion       New database version
     */
    @Override
    public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {
        try {
            // Delete the old version of the User table, and ignore errors
            TableUtils.dropTable(connectionSource, User.class, true);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            // Rereate User table
            TableUtils.createTableIfNotExists(connectionSource, User.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

Query data

Assume that you are to query all data in the User table, and sort the data by timestamp field in an ascending order.

    /**
     * Initialize database 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: User information
     */
    private void insertUser(User user) {
        if (null == user) {
            return;
        }
        try {
            // Insert data, mDbHelper is the DemoOrmLiteSqliteOpenHelper that you customized 
            mDbHelper.getDao(User.class).create(user);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

Delete data

    /**
     * Delete user information
     *
     * @param user: User information
     */
    private void deleteUser(User user) {
        try {
            // Delete data, mDbHelper is the DemoOrmLiteSqliteOpenHelper that you customized
            mDbHelper.getDao(User.class).delete(user);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

OrmLite introduction