簡體   English   中英

在android的sdCard上創建SQLCiphered數據庫時出錯

[英]Error while Creating SQLCiphered database on sdCard in android

我試圖在sdCard中創建一個sqlCipher加密數據庫,然后從那里讀取並存儲值。

這是我的代碼。

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)它要么不創建表,要么其他錯誤,因為long as = mDbHelper.createNote("abc"); 給出錯誤, no such table notes

查看代碼,您將看到:

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

}

這個想法是,您應該用填充數據庫的實際代碼替換// TODO 您可以通過閱讀SQLiteOpenHelper的文檔來SQLiteOpenHelper

您創建一個實現onCreate(SQLiteDatabase),onUpgrade(SQLiteDatabase,int,int)和可選的onOpen(SQLiteDatabase)的子類,並且該類負責打開數據庫(如果存在),創建數據庫(如果不存在)以及根據需要進行升級。 事務用於確保數據庫始終處於明智狀態。

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

因此,我建議您:

  1. 移動SQLiteDatabase.loadLibs(mContext); 給你的構造函數。

  2. 刪除其余的openDataBase()和所有close() (因為該代碼已經存在)

  3. 重寫checkDataBase()以擺脫無效的DB_PATH改用getDatabasePath()

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

  5. 刪除其余的createDataBase()

  6. 在您的活動中正確使用SQLiteOpenHelper

為什么要SQLiteOpenHelper 此類的目的是管理數據庫文件,打開/關閉它們,在必要時創建它們以及管理應用程序數據庫對象( SQLiteDatabase )。

但是,您可以自己執行此操作,而在SQLiteOpenHelper.onCreate中不執行任何操作 ,這意味着SQLiteOpenHelper不執行任何操作。 如果要自己創建數據庫文件,請不要SQLiteOpenHelper

那是什么SQLiteDatabase類? 我沒有看到任何loadLibs的方法官方一個 ,也不openOrCreateDatabase / openDatabase其接受你傳遞參數重載...

暫無
暫無

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

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