簡體   English   中英

如何使用OrmLite連接到受密碼保護的SQLite DB?

[英]How to connect to Password protected SQLite DB with OrmLite?

我通過以下代碼從資產復制數據庫:

    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();
            }
    }
    }

為了得到道,我用這個:

public Dao<AnyItem, Integer> getDaoAnyItem() throws SQLException {
        if (daoAnyItem == null) {
            daoAnyItem = getDao(AnyItem.class);
        }
        return daoAnyItem;
    }

但是,如果我的數據庫將受到密碼保護,如何獲得保護?

您必須將SQLCipher與OrmLite結合使用,我建議您使用ormlite -sqlcipher

OrmLiteSqliteOpenHelper具有使用密碼的構造函數,因此將您的超級調用更改為

super(context, DATABASE_NAME, null, DATABASE_VERSION, (File)null, "DB password goes here");

我會從DatabaseHelper構造函數中調用對copyFromAssets(context)的調用,並在創建DatabaseHelper之前調用它,即應用程序啟動時的第一件事

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM