I copy DB from assets by this code:
public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
private static final String DATABASE_NAME = "database.db";
private static final String DATABASE_PATH = "/data/data/"+BuildConfig.APPLICATION_ID+"/databases/";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
copyFromAssets(context);
}
private void copyFromAssets(Context context) {
boolean dbexist = checkdatabase();
if (!dbexist) {
File dir = new File(DATABASE_PATH);
dir.mkdirs();
InputStream myinput = context.getAssets().open(DATABASE_NAME);
String outfilename = DATABASE_PATH + DATABASE_NAME;
Log.i(DatabaseHelper.class.getName(), "DB Path : " + outfilename);
OutputStream myoutput = new FileOutputStream(outfilename);
byte[] buffer = new byte[1024];
int length;
while ((length = myinput.read(buffer)) > 0) {
myoutput.write(buffer, 0, length);
}
myoutput.flush();
myoutput.close();
myinput.close();
}
}
}
to get Dao I use this:
public Dao<AnyItem, Integer> getDaoAnyItem() throws SQLException {
if (daoAnyItem == null) {
daoAnyItem = getDao(AnyItem.class);
}
return daoAnyItem;
}
But how to get Dao if my DB will be Password protected?
Right-click on your db file in its folder and select "open with..." then select the exe for SQLite2009 Pro, or drag the file onto the exe (assuming you're on Windows), or pass the file path to the exe via the cmd line.
You can password protect a SQLite3 DB. Before doing any operations, set the password as follows. conn = new SQLiteConnection("Data Source=MyDatabase. sqlite;Version=3;Password=password;"); conn.
ORMLite is an Object Relational Mapping package that provides simple and lightweight functionality for persisting Java objects to SQL databases while avoiding the complexity and overhead of more standard ORM packages. Speaking for Android, OrmLite is implemented over the out-of-the-box supported database, SQLite.
To change the database password use the ChangePassword method of SQLiteConnection. To decrypt the database, assign an empty password to it. Here is an example of using dotConnect for SQLite with an SQLiteCrypt encrypted database.
You must use SQLCipher with OrmLite, I would suggest ormlite-sqlcipher library to you
OrmLiteSqliteOpenHelper has a constructor which takes a password so change you super call to
super(context, DATABASE_NAME, null, DATABASE_VERSION, (File)null, "DB password goes here");
I would take the call to copyFromAssets(context) out of the DatabaseHelper constructor and call it before DatabaseHelper gets created i.e. first thing when the app starts up
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With