C# 如何在Android仿真器中访问本地文件夹以检索SQLite db?
我创建了一个SQLite数据库来存储来自假设应用程序的数据,其中用户存储配方:C# 如何在Android仿真器中访问本地文件夹以检索SQLite db?,c#,sqlite,xamarin,android-emulator,C#,Sqlite,Xamarin,Android Emulator,我创建了一个SQLite数据库来存储来自假设应用程序的数据,其中用户存储配方: using System; using System.IO; using DataAccesSQLite_scratch; using SQLite; using Xamarin.Forms; [assembly: Dependency(typeof(SQLiteDb))] namespace DataAccesSQLite_scratch { public class SQLiteDb : ISQLit
using System;
using System.IO;
using DataAccesSQLite_scratch;
using SQLite;
using Xamarin.Forms;
[assembly: Dependency(typeof(SQLiteDb))]
namespace DataAccesSQLite_scratch
{
public class SQLiteDb : ISQLiteDb
{
public SQLiteAsyncConnection GetConnection()
{
var documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
var path = Path.Combine(documentsPath, "MySQLite.db3");
return new SQLiteAsyncConnection(path);
}
}
}
及
使用系统;
使用System.Collections.ObjectModel;
使用系统组件模型;
使用System.Runtime.CompilerServices;
使用SQLite;
使用Xamarin.Forms;
命名空间DataAccesSQLite_scratch
{
公共类配方:INotifyPropertyChanged
{
公共事件属性更改事件处理程序属性更改;
私有void OnPropertyChanged([CallerMemberName]字符串propertyName=null)
{
if(PropertyChanged!=null)
PropertyChanged(这是新的PropertyChangedEventArgs(propertyName));
}
[主密钥,自动增量]
公共int Id{get;set;}
私有字符串\u名称;
[MaxLength(225)]
公共字符串名
{
获取{return\u name;}
设置
{
//下面的代码确保如果值未更改,则不应调用此事件
如果(_name==值)
返回;
_名称=值;
OnPropertyChanged();
}
}
}
公共部分类主页:ContentPage
{
专用SQLiteAsyncConnection\u连接;
私人可观察收集配方;
公共主页()
{
初始化组件();
_connection=DependencyService.Get().GetConnection();
}
//按照惯例,使用“OnAppearing()”方法,不要将底层代码放入构造函数中
受保护的重写异步void OnAppearing()
{
wait_connection.CreateTableAsync();
var recipes=wait_connection.Table().ToListAsync();
_配方=新的可观察收集(配方);
recipesListView.ItemsSource=\u配方;
base.OnAppearing();
}
异步void OnAdd(对象发送方,System.EventArgs e)
{
var recipe=new recipe{Name=“recipe”+DateTime.Now.Ticks};
等待连接。插入同步(配方);
_配方。添加(配方);
}
异步void OnUpdate(对象发送方,System.EventArgs e)
{
var recipe=_recipes[0];
配方名称+=“已更新”;
wait_connection.UpdateAsync(配方);
}
异步void OnDelete(对象发送方,System.EventArgs e)
{
var recipe=_recipes[0];
等待连接。删除异步(配方);
_配方。移除(配方);
}
}
}
所有方法均有效,并妥善保存。因此,当我关闭应用程序并再次启动它时,数据将成功地持久化。但是,如何在模拟器中访问这些数据?然后我如何将这些数据导出为.csv或.json格式?可以看到,它应该在SpecialFolder.MyDocuments中。但是我在模拟器上找不到它
提前谢谢
但是,如何在模拟器中访问这些数据?然后我如何将这些数据导出为.csv或.json格式?可以看到,它应该在SpecialFolder.MyDocuments中。但是我在模拟器上找不到它
根据您的sqlite数据库路径,它是内部存储,您无法在仿真器上找到数据库文件,除非它是根数据库。然后可以在data/data/com.companyname/files中找到数据库文件
打开cmd,向Root android emulator输入Adb Root,然后输入Adb shell验证是否成功
如果要将sqlite数据库导出到csv文件,请查看以下线程:
它位于应用程序的安全沙箱中,使用可根目录的仿真器映像(即“系统映像”,而不是“Google API映像”或“Google Play映像”),或将db文件复制/保存到非安全位置,如:@Viol1997是否有任何更新?
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using SQLite;
using Xamarin.Forms;
namespace DataAccesSQLite_scratch
{
public class Recipe : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
private string _name;
[MaxLength(225)]
public string Name
{
get { return _name; }
set
{
// code below assures that if the value isn't changed, this event should not be called
if (_name == value)
return;
_name = value;
OnPropertyChanged();
}
}
}
public partial class MainPage : ContentPage
{
private SQLiteAsyncConnection _connection;
private ObservableCollection<Recipe> _recipes;
public MainPage()
{
InitializeComponent();
_connection = DependencyService.Get<SQLiteDb>().GetConnection();
}
// By convention use the "OnAppearing()" method and don't put underlying code in the constructor
protected override async void OnAppearing()
{
await _connection.CreateTableAsync<Recipe>();
var recipes = await _connection.Table<Recipe>().ToListAsync();
_recipes = new ObservableCollection<Recipe>(recipes);
recipesListView.ItemsSource = _recipes;
base.OnAppearing();
}
async void OnAdd(object sender, System.EventArgs e)
{
var recipe = new Recipe { Name = "Recipe" + DateTime.Now.Ticks };
await _connection.InsertAsync(recipe);
_recipes.Add(recipe);
}
async void OnUpdate(object sender, System.EventArgs e)
{
var recipe = _recipes[0];
recipe.Name += " UPDATED";
await _connection.UpdateAsync(recipe);
}
async void OnDelete(object sender, System.EventArgs e)
{
var recipe = _recipes[0];
await _connection.DeleteAsync(recipe);
_recipes.Remove(recipe);
}
}
}