【问题标题】:How to access a database file on android device [Xamarin App with Entity Framework for SQLite]如何访问 android 设备上的数据库文件 [Xamarin App with Entity Framework for SQLite]
【发布时间】:2020-02-10 07:22:50
【问题描述】:

我是移动编程的初学者,我正在尝试在 Xamarin Forms 中创建一个数据库应用程序,有一个实体框架 SQLite 可以轻松使用数据库。阅读文章后,我将我的应用程序配置为:

public partial class App : Application
    {
        public const string DbFileName = "DailyAppDatabase.db";
        string dbPath = DependencyService.Get<IPath>().GetDatabasePath(DbFileName);

        public App()...
     }

在 android 项目中,我实现了一个返回数据库路径的方法:

return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), filename);

this metod returns "/data/user/0/com.companyname.daily_app/files/DailyAppDatabase.db"

我想我应该在这里搜索一个数据库文件(我想得到这个文件并在 SQLite DB 浏览器中打开它)。 但我不能使用模拟器,我的虚拟化系统无法正常工作,我在物理 unrooted Android 7.0 设备上调试我的应用程序。 我该怎么做? 我看过很多类似的主题:

How to access data/data folder in Android device?

Retrieve database or any other file from the Internal Storage using run-as

找不到答案,我真的需要帮助,我尝试了以下方法:

1) using adb shell command chmod 666 /data/user/0/com.companyname.daily_app/files/DailyAppDatabase.db

2)adb shell -&gt; run-as com.your.packagename -&gt; cp /data/data/com.your.packagename/

但是我的设备上这些命令的结果是权限被拒绝,我尝试了这个方法:

     string _databasePath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "DailyAppDatabse.db");      
            if (!File.Exists(_databasePath))
                return;
            Stream myInput = new FileStream(_databasePath, FileMode.Open);
            Stream myOutput = Assets.Open("database.db");

            byte[] buffer = new byte[1024];
            int b = buffer.Length;
            int length;
            while ((length = myInput.Read(buffer, 0, b)) > 0)
            {
                myOutput.Write(buffer, 0, length);
            }
            myOutput.Flush();
            myOutput.Close();
            myInput.Close();

但我认为,这是解决问题的错误方法,获取异常“方法 myOutput.write (...) is not supported. 也许有人遇到了这个问题并且可以提供帮助,我不想获得 root 访问权限,我喜欢 EntityFramework Core for SQLite 中的代码优先方法

【问题讨论】:

标签: android database sqlite xamarin.forms entity-framework-core


【解决方案1】:

我找到了解决方案,这对我来说太难了。 以下算法:

1) 制作备份文件(注意应用清单中的属性 android:allowBackup = true)

adb backup -f "\location\on\local\drive" -noapk app.package.name

2) 开箱.ab file to .tar file using abe.jar

3) .tar 文件夹中有文件和db文件

也许这会对将来的某人有所帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-28
    • 2016-11-25
    • 1970-01-01
    • 1970-01-01
    • 2018-07-06
    • 2021-09-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多