[英]Using SQLite database in Xamarin.Forms
我已經創建了一個SQLite數據庫(使用cmd shell sqlite3.exe),現在正嘗試使用此頁面在Xamarin.Forms中實現它https://developer.xamarin.com/guides/android/application_fundamentals/data/part_3_using_sqlite_orm/指導。 不過,我很困惑,因為我不知道數據庫的路徑是什么。 我將數據庫命名為“ app”,表命名為“ tbl1”,但這就是我所知道的所有信息。 該數據庫僅存儲用戶名和密碼,並且僅在我的跨平台應用程序的登錄頁面上使用。
使用Xamarin.Forms
(您的鏈接適用於Android!),您將必須從共享項目中使用某種抽象來訪問數據庫(特定於平台)。 這就是您基本上可以在每個平台上獲取正確的存儲路徑的方式。
這基本上意味着在共享項目中創建一個接口,即:
public interface IFileHelper
{
string GetLocalFilePath(string filename);
}
然后,您必須實現平台特定的部分。 即Android:
[assembly: Dependency(typeof(FileHelper))]
namespace Todo.Droid
{
public class FileHelper : IFileHelper
{
public string GetLocalFilePath(string filename)
{
string path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
return Path.Combine(path, filename);
}
}
}
iOS版:
[assembly: Dependency(typeof(FileHelper))]
namespace Todo.iOS
{
public class FileHelper : IFileHelper
{
public string GetLocalFilePath(string filename)
{
string docFolder = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
string libFolder = Path.Combine(docFolder, "..", "Library", "Databases");
if (!Directory.Exists(libFolder))
{
Directory.CreateDirectory(libFolder);
}
return Path.Combine(libFolder, filename);
}
}
}
UWP:
using Windows.Storage;
...
[assembly: Dependency(typeof(FileHelper))]
namespace Todo.UWP
{
public class FileHelper : IFileHelper
{
public string GetLocalFilePath(string filename)
{
return Path.Combine(ApplicationData.Current.LocalFolder.Path, filename);
}
}
}
然后檢索和使用最簡單的方法是使用Xamarin的DependencyService
。 更多信息在這里 。
這是有關本地數據庫的官方文檔 。
希望能幫助到你!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.