Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect to Password protected SQLite DB with OrmLite?

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?

like image 437
NickUnuchek Avatar asked Apr 27 '17 10:04

NickUnuchek


People also ask

How do I open an encrypted database in SQLite?

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.

Can SQLite be password protected?

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.

What is ORMLite in Android?

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.

How do I change my SQLite password?

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.


2 Answers

You must use SQLCipher with OrmLite, I would suggest ormlite-sqlcipher library to you

like image 164
MBN Avatar answered Sep 19 '22 22:09

MBN


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

like image 31
Pete Hendry Avatar answered Sep 16 '22 22:09

Pete Hendry