简体   繁体   English

引用和读取.sqlite数据库文件(C#/ UWP)

[英]Referencing and reading from a .sqlite database file (C#/UWP)

All I'm trying to do is use a .sqlite database file that I have stored in the Assets folder of the app (/Assets/CommonCore.sqlite). 我要做的只是使用存储在应用程序(/Assets/CommonCore.sqlite)的Assets文件夹中的.sqlite数据库文件。 I'm trying to set up the connection and I've tried a few different things: 我正在尝试建立连接,并且尝试了一些其他操作:

    SQLite.Net.SQLiteConnection conn;

    public MainPage()
    {
        this.InitializeComponent();

        conn = new SQLite.Net.SQLiteConnection("Data Source=Assets/CommonCore.sqlite");

but this throws an exception "does not contain a constructor that takes 1 arguments". 但这会引发异常“不包含带有1个参数的构造函数”。

    string path;
    SQLite.Net.SQLiteConnection conn;

    public MainPage()
    {
        this.InitializeComponent();

        path = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "CommonCore.sqlite");

        conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), path);

but that references a folder "\\AppData\\Local\\Packages\\8ed84aca-896d-4286-ba91-e52316db2e89_dzc2ymb8n4xk4\\LocalState\\CommonCore.sqlite" which obviously isn't the file I need. 但这引用的文件夹“ \\ AppData \\ Local \\ Packages \\ 8ed84aca-896d-4286-ba91-e52316db2e89_dzc2ymb8n4xk4 \\ LocalState \\ CommonCore.sqlite”显然不是我需要的文件。

I'm sure it's something silly I'm missing. 我敢肯定,我很想念这件事。

In UWP apps, the folders that you can access are limited: 在UWP应用中,您可以访问的文件夹受到限制:

  • All the files that a user specifies using the FileOpenPicker or FolderPicker 用户使用FileOpenPicker或FolderPicker指定的所有文件
  • Files from the FutureAccessList or MostRecentlyUsedList. FutureAccessList或MostRecentlyUsedList中的文件。 The FutureAccessList can be declared in the manifest file (eg Documents, Pictures, Videos folder). 可以在清单文件(例如,Documents,Pictures,Videos文件夹)中声明FutureAccessList。 However, the user has to allow access to these folders. 但是,用户必须允许访问这些文件夹。
  • Files which are opened with a file extension association or via sharing. 通过文件扩展名关联或共享打开的文件。
  • The application install directory. 应用程序安装目录。
  • The application data locations. 应用程序数据位置。
  • In some cases removable devices. 在某些情况下,可移动设备。

Source 资源

In your case you were trying to write to the application data locations. 在您的情况下,您试图写入应用程序数据位置。 However, each time you install your app, this folder is empty. 但是,每次安装应用程序时,该文件夹都是空的。

An alternative would be to read the sqlite database from the install folder. 一种替代方法是从安装文件夹中读取sqlite数据库。 It is easy to add files from your project to this folder. 将项目中的文件添加到此文件夹很容易。 To do this, you first have to declare which files you want to package. 为此,您首先必须声明要打包的文件。 This means adding them to the install folder. 这意味着将它们添加到安装文件夹。 First, create a subfolder in your work directory (the folder where you can find the AppxManifest.xml). 首先,在工作目录(可以找到AppxManifest.xml的文件夹)中创建一个子文件夹。 This is not necessary but cleaner. 这不是必需的,但更干净。 I've called my folder 'Data'. 我将文件夹称为“数据”。

Next you can check if you did this correctly. 接下来,您可以检查是否正确执行了此操作。 Open your project in visual studio and click the 'Show all files' button in the solution explorer. 在Visual Studio中打开您的项目,然后在解决方案资源管理器中单击“显示所有文件”按钮。

“显示所有文件”按钮

After that, you should be able to see the new folder and its content in the solution explorer. 之后,您应该能够在解决方案资源管理器中看到新文件夹及其内容。 You can now right click on the folder and select 'Include in project'. 现在,您可以右键单击该文件夹,然后选择“包含在项目中”。

包含在项目中

Finally, you should open the properties of your file (eg db.sqlite in the Data folder) and set the build action to content. 最后,您应该打开文件的属性(例如,Data文件夹中的db.sqlite),并将构建操作设置为content。

设定内容

Now you can connect to the db using the code: 现在,您可以使用以下代码连接到数据库:

public class Database
{
    string path;
    SQLite.Net.SQLiteConnection conn;

    public Database()
    {
        path = Path.Combine(Windows.ApplicationModel.Package.Current.InstalledLocation.Path, "Data", "Questions.sqlite");
        conn = new SQLite.Net.SQLiteConnection(new
           SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), path);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM