O armazenamento de banco de dados do mPaaS criptografa a camada subjacente com base na arquitetura OrmLite. Use as interfaces a seguir para adicionar, excluir, modificar e consultar dados.
Baselines 10.2.3 e posteriores:
com.alibaba.j256.ormlite.dao.DaoBaseline 10.1.68 e anteriores:
com.j256.ormlite.dao.Dao
Não criptografe diretamente o banco de dados original, pois isso causa falha na descriptografia da camada nativa. Crie um novo banco de dados criptografado e copie o conteúdo do banco original para ele.
Exemplos
Gerar tabelas
// 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;
}
Criar OrmLiteSqliteOpenHelper
Crie uma classe personalizada DemoOrmLiteSqliteOpenHelper que herde de OrmLiteSqliteOpenHelper.
A classe OrmLiteSqliteOpenHelper permite criar e criptografar bancos de dados.
Baselines 10.2.3 e posteriores:
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();
}
}
}
Baseline 10.1.68 e anteriores:
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();
}
}
}
Consultar dados
Para consultar todos os dados da tabela User e ordená-los pelo campo timestamp em ordem crescente, use o código abaixo.
/**
* 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();
}
}
Inserir dados
/**
* 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();
}
}
Excluir dados
/**
* 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();
}
}