El almacenamiento de base de datos de mPaaS permite cifrar la capa subyacente de la base de datos con base en la arquitectura OrmLite. Llame a la siguiente interfaz para agregar, eliminar, modificar y consultar datos en las bases de datos.
Líneas base 10.2.3 y posteriores:
com.alibaba.j256.ormlite.dao.DaoLínea base 10.1.68 y anteriores:
com.j256.ormlite.dao.Dao
Al usar la base de datos, no cifre directamente la base de datos original; de lo contrario, se producirá un fallo en el descifrado de la capa nativa. Se recomienda crear primero una nueva base de datos cifrada y luego copiar el contenido de la base de datos original a la nueva base de datos cifrada.
Ejemplos
Generar tablas
// 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;
}
Crear OrmLiteSqliteOpenHelper
Personalice una clase DemoOrmLiteSqliteOpenHelper que herede de OrmLiteSqliteOpenHelper.
Con OrmLiteSqliteOpenHelper, puede crear y cifrar una base de datos.
Líneas base 10.2.3 y 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();
}
}
}
Línea base 10.1.68 y 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 datos
Suponga que desea consultar todos los datos de la tabla User y ordenarlos por el campo timestamp en orden ascendente.
/**
* 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();
}
}
Insertar datos
/**
* 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();
}
}
Eliminar datos
/**
* 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();
}
}