为了账号安全,请及时绑定邮箱和手机立即绑定

如何在没有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 关注
  • 107 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信