【问题标题】:How to prepare Sqlite database for cross platform use如何准备 Sqlite 数据库以供跨平台使用
【发布时间】:2016-07-19 19:10:34
【问题描述】:

我正在构建一个 Xamarin 跨平台应用程序。我希望我的应用程序从初始数据库开始。我如何创建我的 sqlite 数据库有关系吗?我的意思是我可以使用一些 SQlite 浏览器应用程序来创建它并期望它在 Android、IOS 和 UWP 中工作吗?我应该关心每个平台使用的 sqlite 版本吗?

【问题讨论】:

  • 我通常使用 FF SQLiteManager 插件来创建数据库,然后将其复制到我的各种项目中。它应该很简单。我从来没有遇到过版本冲突
  • @Jason 当你在各种平台项目中包含数据库文件时,你的数据库文件存放在哪个文件夹?
  • iOS 和 WinPhone 可以放在根目录下,Android 放在 Assets 中
  • @Jason 在 Android 中,如何获取 Assets 文件夹的路径?我看到的所有示例都使用 AssetManager 从 Assets 中读取,但我没有看到任何显示如何获取资产路径的示例?
  • 在我的情况下,我将它从资产复制到可写文件夹。

标签: sqlite xamarin


【解决方案1】:

在 iOS 上,要复制添加到 Xamarin iOS 项目根目录的数据库,请在 AppDelegate.FinsishedLaunching 方法中运行此代码:

var dbPath = Path.Combine (System.Environment.GetFolderPath (System.Environment.SpecialFolder.Personal), "yourdatabase.db3");
var appDir = NSBundle.MainBundle.ResourcePath;
var seedFile = Path.Combine(appDir, "yourdatabase.db3");
if (!File.Exists(dbPath) && File.Exists(seedFile))
{
    File.Copy(seedFile, dbPath);
}

对于 Android,将数据库文件放在 Resources 文件夹中名为 Raw 的文件夹中(如果 Raw 文件夹不存在,请创建它)。还要确保将ReadExternalStorageWriteExternalStorage 权限添加到AndroidManifest.xml 文件中。然后在 MainActivity.OnCreate 方法中运行以下代码:

var dbPath = Path.Combine (System.Environment.GetFolderPath (System.Environment.SpecialFolder.Personal), "yourdatabase.db3"); 
var readStream = Resources.OpenRawResource(Resource.Raw.yourdatabase);
if (!System.IO.File.Exists(dbPath)) {
    FileStream writeStream = new FileStream(dbPath, FileMode.OpenOrCreate, FileAccess.Write);
    ReadWriteStream(readStream, writeStream);
}

并将以下方法添加到您的 MainActivity 类中:

private void ReadWriteStream(Stream readStream, Stream writeStream)
{
    int Length = 256;
    Byte[] buffer = new Byte[Length];
    int bytesRead = readStream.Read(buffer, 0, Length);
    // write the required bytes
    while (bytesRead > 0)
    {
        writeStream.Write(buffer, 0, bytesRead);
        bytesRead = readStream.Read(buffer, 0, Length);
    }
    readStream.Close();
    writeStream.Close();
}

然后您的数据库文件将位于dbPath 变量路径中的可写位置。请注意,此代码假定您只想复制 db 文件,如果它在可写位置不存在,这似乎是一个有效的假设。 :-)

【讨论】:

    【解决方案2】:

    也许这个链接可以帮助你,

    https://developer.xamarin.com/guides/xamarin-forms/working-with/databases/

    请注意,此链接显示了如何使用 xamarin.Forms 创建本地数据库,并根据每个平台获取 dbpath。如果您使用的是 mvvmcross,我建议您不要使用此解决方案。

    我正在寻找一种在不使用 xamarin.Forms 的情况下根据每个平台获取本地数据库路径的方法,因为 xamarin.Forms 依赖于旧版本的 android.support.v4/v7,而这在我的情况下是导致其他问题,比如无法安装下载缓存插件。

    在同一个链接中,我提供了我正在尝试的信息,但该代码不适用于 mvvmcross v4.4.0。在//winPhone 中无法识别

    ApplicationData.Current.LocalFolder.Path

    string DatabasePath {
        get {
            var sqliteFilename = "TodoSQLite.db3";
            #if __IOS__
            string documentsPath = Environment.GetFolderPath (Environment.SpecialFolder.Personal); // Documents folder
            string libraryPath = Path.Combine (documentsPath, "..", "Library"); // Library folder
            var path = Path.Combine(libraryPath, sqliteFilename);
            #else
            #if __ANDROID__
            string documentsPath = Environment.GetFolderPath (Environment.SpecialFolder.Personal); // Documents folder
            var path = Path.Combine(documentsPath, sqliteFilename);
            #else
            // WinPhone
            var path = Path.Combine(ApplicationData.Current.LocalFolder.Path, sqliteFilename);
    
            //TODO: Add Windows 8.1 and UWP cases here!
            // (use the PCL code above)
    
            #endif
            #endif
            return path;
        }
    

    这是我的经验并不是真正的答案,但也许我帮助了你或者你可以帮助我;)谢谢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-12
      • 2016-11-12
      • 2022-01-04
      • 2014-01-31
      • 1970-01-01
      • 2011-08-26
      相关资源
      最近更新 更多