->好的,这里我在SD卡中有一个Sq-lite数据库。

  (String DB_PATH = Environment.getExternalStorageDirectory().getPath().toString();)


->检查数据库是否存在

        dbFile = new File(DB_PATH + DB_NAME );
        if(dbFile.exists()== true)
        {
            //set text db exists
            open_DB();

        }
        else
        {
            //set text db does not exist
            return;
        }


->在调试时,我可以确认数据库文件的存在。但是当打开
    数据库我得到异常

    SQLiteDatabase checkDB = null;
    try
    {
        dbFile = new File(DB_PATH + DB_NAME);
        myDataBase = SQLiteDatabase.openOrCreateDatabase(dbFile, null);

        //myDataBase = SQLiteDatabase.openDatabase(DB_PATH + DB_NAME, null, 0);
        //also tried this

        //set text bd opened
    }
    catch(SQLiteException e)
    {
        //set text bd unable open
        return;
    }

最佳答案

可能是您的问题是由标志或文件引起的,请尝试使用此方法:

dbFile = new File(DB_PATH + DB_NAME);
if (dbFile.exists()) {
   db = SQLiteDatabase.openDatabase(db_path, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.CREATE_IF_NECESSARY);
}


让我知道。

08-06 06:32