2017-04-27 117 views
10

我复制DB从资产这段代码:如何使用OrmLite连接到受密码保护的SQLite数据库?

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

但如果我的DB将被密码保护如何让道?

回答

2

OrmLiteSqliteOpenHelper有一个构造函数,需要一个密码,这样超级呼叫改变你

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

我想借此呼吁copyFromAssets(context)从DatabaseHelper构造函数中取出并在DatabaseHelper创建之前调用它,即应用程序启动时的第一件事

+0

它没有这个构造函数'super(context,DATABASE_NAME,nul l,DATABASE_VERSION,(File)null,“DB password goes here”);' – NickUnuchek

+0

然后你没有使用omrlite-sqlcipher,请参阅前面的答案 –