【发布时间】: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 -> run-as com.your.packagename -> 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 中的代码优先方法
【问题讨论】:
-
Xamarin 推荐的 SQLite.NET 库是一个非常基本的 ORM,可让您轻松地在 Android 设备上的本地 SQLite 数据库中存储和检索对象。因此您可以安装 sqlite-net- pcl 通过 Nuget 包在你的项目中,有一些关于使用它的文章:docs.microsoft.com/en-us/xamarin/android/data-cloud/data-access/… 和 c-sharpcorner.com/article/xamarin-android-sqlite-database
标签: android database sqlite xamarin.forms entity-framework-core