繁体   English   中英

从Android中的外部存储访问或打开数据库

[英]Access or open database from external storage in android

->好的,这里我在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);
}

让我知道。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM