亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在沒有root的情況下訪問Android應用程序上的數據庫

如何在沒有root的情況下訪問Android應用程序上的數據庫

湖上湖 2023-06-04 15:20:53
我正在開發一個包含 100 個元素的數據庫的小型應用程序。我導入了數據庫,但只有一個模擬器(我擁有的 3 個女巫)運行正常。我發現它運行沒有問題,因為“Songs.db”數據庫存在于 data/data/myapppackage/databases/ 文件夾中,如果不對設備進行 root,我將無法訪問。我通過互聯網搜索解決此問題的不同方法和解決方案,但沒有任何效果。我是 android 編程的新手,對于這種問題,沒有任何教程。public class DatabaseHelper extends SQLiteOpenHelper {    public static final String DATABASE_NAME = "Songs.db";    public static final String TABLE_NAME = "songs_table";    public static final String COL_1 = "ID";    public static final String COL_2 = "TITLE";    public DatabaseHelper (Context context) {        super( context, DATABASE_NAME, null, 1 );        SQLiteDatabase db = this.getWritableDatabase();    }    public Cursor getData(int id) {        SQLiteDatabase db = this.getReadableDatabase();        Cursor res =  db.rawQuery( "select TITLE from songs_table where ID="+id+"", null );        return res;    }}和 PlayerTurn 類myDb = new DatabaseHelper( this );Cursor rs = db.getData( b );rs.moveToFirst();tit = rs.getString( rs.getColumnIndex( db.COL_2 ) );我大多數時候收到的錯誤消息是android.database.sqlite.SQLiteException: no such table: songs_table (code 1): 誰能幫幫我?我花了將近 15 個小時...
查看完整描述

1 回答

?
拉莫斯之舞

TA貢獻1820條經驗 獲得超10個贊

您可以將數據庫復制到 SD 卡中,您可以隨時從 SD 卡訪問數據庫


試試這個代碼:


 try {

    File sd = Environment.getExternalStorageDirectory();

    File data = Environment.getDataDirectory();

    if (sd.canWrite()) {

        String currentDBPath = "data/"+sPackageName+"/databases/"+sDBName;

        String backupDBPath = "/.appname-external-data-cache/"+sDBName; //"{database name}";

        File dir = new File(sd,backupDBPath.replace(sDBName,""));

        if(dir.mkdir()) {


        }

        File currentDB = new File(data, currentDBPath);

        File backupDB = new File(sd, backupDBPath);

        if (currentDB.exists()) {

            FileChannel src = new FileInputStream(currentDB).getChannel();

            FileChannel dst = new FileOutputStream(backupDB).getChannel();

            dst.transferFrom(src, 0, src.size());

            src.close();

            dst.close();

        }

    } 


   } catch (Exception e) {

}


查看完整回答
反對 回復 2023-06-04
  • 1 回答
  • 0 關注
  • 197 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號