2012-11-22 41 views
1

我想在SD卡中創建一個sqlCipher加密數據庫,然後從那裏讀取和存儲值。錯誤,而在SD卡上創建SQLCiphered數據庫在android

這是我的代碼。

public class DataBaseHelper extends SQLiteOpenHelper 
{ 
    private static String TAG = "DataBaseHelper"; // Tag just for the LogCat window 
    private static String DB_PATH ;//path of our database 
    private static String DB_NAME = "application-database";// Database name 
    private static int DATABASE_VERSION = 1; 

    private SQLiteDatabase mDataBase; 
    private final Context mContext; 

    private static final String DATABASE_CREATE_TABLE1 = 
      "create table notes (_id integer primary key autoincrement, myval);"; 


    public DataBaseHelper(Context context) 
    { 
     super(context, DB_NAME, null, DATABASE_VERSION);  
     this.mContext = context; 
     DB_PATH = Environment.getExternalStorageDirectory() + "/Personal Folder/"; 
    } 

    public void createDataBase() throws IOException 
    { 
     //If database not exists create it from the assets 
     boolean mDataBaseExist = checkDataBase(); 
     if(!mDataBaseExist) 
     { 
      try 
      { 
       File dbFile = new File(DB_PATH + DB_NAME); 
       SQLiteDatabase.loadLibs(mContext); 
       dbFile.mkdirs(); 
       dbFile.delete(); 
       SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbFile, "setPassword", null); 
       db.execSQL(DATABASE_CREATE_TABLE1); 
       Log.e(TAG, "createDatabase database created"); 
      } 
      catch (SQLException mIOException) 
      { 
       throw new Error("Error Creating Database"); 
      } 
     } 
    } 

    private boolean checkDataBase() 
    { 
     File dbFile = new File(DB_PATH + DB_NAME); 
     return dbFile.exists(); 
    } 

    //Open the database, so we can query it 
    public boolean openDataBase() throws SQLException 
    { 
     String mPath = DB_PATH + DB_NAME; 
     SQLiteDatabase.loadLibs(mContext); 
     mDataBase = SQLiteDatabase.openDatabase(mPath, "setPassword", null, SQLiteDatabase.CREATE_IF_NECESSARY); 
     return mDataBase != null; 
    } 

    @Override 
    public synchronized void close() 
    { 
     if(mDataBase != null) 
      mDataBase.close(); 
     super.close(); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
    { 
     Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data"); 

     /* 
     if (oldVersion == 2) 
     { 
      db.execSQL("ALTER TABLE notes ADD " + KEY_DATA + " blog"); 
      db.execSQL("ALTER TABLE notes ADD " + KEY_TYPE + " text"); 

     } 

     if (newVersion == 3) 
     { 
      db.execSQL("ALTER TABLE notes ADD " + KEY_TYPE + " text"); 
     } 
     */ 
    } 

    @Override 
    public void onCreate(SQLiteDatabase arg0) { 
     // TODO Auto-generated method stub 

    } 

} 


public class PADatabaseAdapter 
{ 
    private static final String TAG = "DbAdapter"; 
    private final Context mContext; 
    private DataBaseHelper mDbHelper; 
    private SQLiteDatabase mDb; 

    /** 
    * Database creation sql statement 
    */ 
    public PADatabaseAdapter(Context ctx) 
    { 
     this.mContext = ctx; 
     mDbHelper = new DataBaseHelper(mContext); 
    } 

    public PADatabaseAdapter createDatabase() throws SQLException 
    { 
     try 
     { 
      mDbHelper.createDataBase(); 
     } 
     catch (IOException mIOException) 
     { 
      Log.e(TAG, mIOException.toString() + " UnableToCreateDatabase"); 
      throw new Error("UnableToCreateDatabase"); 
     } 
     return this; 
    } 

    public PADatabaseAdapter open(String password) throws SQLException 
    { 
     try 
     { 
      mDbHelper.openDataBase(password); 
      //mDbHelper.close(); 
      mDb = mDbHelper.getmDataBase(); 
        // mDbHelper.getWritableDatabase(password); 

     } 
     catch (SQLException mSQLException) 
     { 
      Log.e(TAG, "open >>"+ mSQLException.toString()); 
      throw mSQLException; 
     } 
     System.gc(); 
     return this; 
    } 

    public boolean isOpen() 
    { 
     if (mDb !=null) 
      return mDb.isOpen(); 
     else 
      return false; 
    } 

    public void rekey (String password) 
    { 
     mDb.execSQL("PRAGMA rekey = '" + password + "'"); 
     System.gc(); 
    } 

    public void close() 
    { 
     mDb.close(); 
     mDbHelper.close(); 
    } 

這是我用我的活動

mContext = this; 
    mDbHelper = new PADatabaseAdapter(this);   
    mDbHelper.createDatabase(); 

    mDbHelper.open("setPassword"); 
    long as = mDbHelper.createNote("abc"); 
    mDbHelper.close(); 

    mDbHelper.open("setPassword"); 
    Cursor mCursor = mDbHelper.fetchAllNotes(); 
    mDbHelper.close(); 

的問題是代碼,在db.exec(CREATE_tABLE)它要麼不CREATE TABLE或別的東西是錯誤的,因爲long as = mDbHelper.createNote("abc");給出錯誤no such table notes

+0

穆罕默德嗨具有u解決了這個問題,我也faceing同樣的問題 – sravan

回答

1

如果你看看你的代碼,你會看到:

@Override 
public void onCreate(SQLiteDatabase arg0) { 
    // TODO Auto-generated method stub 

} 

這個想法是,你應該用填充數據庫的實際代碼替換// TODO。可以這麼說,reading the documentation for SQLiteOpenHelper

創建實施的onCreate(SQLiteDatabase),onUpgrade(SQLiteDatabase,INT,INT)和可選的OnOpen(SQLiteDatabase)一個子類,這個類需要如果存在打開數據庫的照顧,如果沒有創建它,並根據需要進行升級。事務用於確保數據庫始終處於明智狀態。

然後您使用getReadableDatabase()getWritableDatabase()方法SQLiteOpenHelper訪問您的數據庫。對於這些方法的Android版本的SQLCipher,您將密碼作爲參數傳遞。

因此,我建議你:

  1. 移動SQLiteDatabase.loadLibs(mContext);到你的構造。

  2. 刪除的openDataBase()休息,所有的close()(因爲代碼已經存在)

  3. 重寫checkDataBase()擺脫無效DB_PATHuse getDatabasePath() instead

  4. 移動db.execSQL(DATABASE_CREATE_TABLE1);onCreate()

  5. 刪除createDataBase()的其餘部分

  6. 使用SQLiteOpenHelper如何正確在活動

+0

我剛纔說另一個類,我只是錯過了,或者我dono爲什麼其實我不想使用手機的內部存儲,因爲數據庫中的數據可能超過1GB。在我的代碼中,以前沒有顯示過類,請檢查編輯後的代碼。不過,我設置私有SQLiteDatabase mDataBase的可見性;向公衆開放並使得吸收者和制定者工作良好。請你再次查看代碼,如果這是正確的,給我一些建議。 –

+0

@umar:「其實我不想使用手機的內部存儲,因爲數據庫中的數據可能超過1GB」 - 是嗎? 「如果這是正確的,請您再次查看代碼並給我一些建議。」 - 你沒有遵循我的任何指示,你的代碼顯然不是你正在運行的('DB_PATH'是'null')。因此,我無法幫助你進一步。 – CommonsWare

0

你爲什麼要繼承SQLiteOpenHelper?這個類的目的是管理數據庫文件,打開/關閉它們,在必要時創建它們並管理應用程序數據庫對象(SQLiteDatabase)。

你這樣做你自己,而這樣做的SQLiteOpenHelper.onCreate什麼這意味着SQLiteOpenHelper什麼都不做。如果您自己創建數據庫文件,則不要繼承SQLiteOpenHelper

什麼是SQLiteDatabase類?我沒有看到任何offical one方法loadLibs,也不openOrCreateDatabase/openDatabase重載其接受你傳遞參數...

+0

如果您閱讀代碼,我正在使用SQLCipher而不是SQLITE :) –

+0

您不需要繼承'SQLiteOpenHelper'然後 – Jong