如何在UWP项目中使用预先设计的SQLite?

我有一个来自另一个项目的sqlite数据库,我想在UWP应用程序中使用它.但我不知道如何将这个数据库导入到项目中!

I can create a new Database and use it but I don’t kNow how to copy database file from project. I use from sqlite.Net-PCL nuget package.

有关如何访问现有文件,所有应用程序都可以访问两个位置.详情请参考file-access-permissions.

一个是Application安装目录.正如@Henk Holterman所说,您可以通过右键单击一个文件夹并选择Add-> Existing item将您现有的数据库文件导入项目,将数据库文件添加到项目中.注意文件的Build action属性需要设置为content.详情请见下图.

假设您已经将数据库文件Sun.db添加到Assets文件夹中,现在您可以通过以下代码连接到它(使用sqlite.Net-PCL Nuget包).

path = Path.Combine(Windows.ApplicationModel.Package.Current.InstalledLocation.Path,@"Assets\Sun.db");
  using (sqlite.Net.sqliteConnection conn = new sqlite.Net.sqliteConnection(new sqlite.Net.Platform.WinRT.sqlitePlatformWinRT(),path))
  {
  }

但是这个文件夹是只读的.另一个位置是可以读/写的应用程序数据位置.您可以将数据库文件从安装目录复制到应用程序数据目录以供使用.以下代码示例用于连接本地文件夹中的数据库文件.

StorageFile file;
  try
  {
      file = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync("Sun.db");
  }
  catch
  {
      StorageFile Importedfile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/Sun.db"));
      file = await Importedfile.copyAsync(Windows.Storage.ApplicationData.Current.LocalFolder);
  }
  path = file.Path;

  using (sqlite.Net.sqliteConnection conn = new sqlite.Net.sqliteConnection(new sqlite.Net.Platform.WinRT.sqlitePlatformWinRT(),path))
  {
      conn.CreateTable<User>();
  }

相关文章

SQLite架构简单,又有Json计算能力,有时会承担Json文件/RES...
使用Python操作内置数据库SQLite以及MySQL数据库。
破解微信数据库密码,用python导出微信聊天记录
(Unity)SQLite 是一个软件库,实现了自给自足的、无服务器...
安卓开发,利用SQLite实现登陆注册功能