简体   繁体   English

在 WinRT 上创建新的 SQLite 数据库?

[英]Create new SQLite database on WinRT?

I want to create a new database from C# code in my WinRT app.我想从我的 WinRT 应用程序中的 C# 代码创建一个新数据库。 I searched the Web and the way to do that appears to be the SQLiteConnection.CreateFile() method.我在网上搜索了一下,这样做的方法似乎是 SQLiteConnection.CreateFile() 方法。 However, that method does not exist in the SQLite namespace, at least in the WinRT version.但是,该方法在 SQLite 命名空间中不存在,至少在 WinRT 版本中是这样。 I installed SQLite using the NuGet packge name "sqlite-net" and included the sqlite3.dll into my project.我使用 NuGet 包名称“sqlite-net”安装了 SQLite,并将 sqlite3.dll 包含到我的项目中。 I do see properly methods like CreateTable(), Query(), etc. but not CreateFile().我确实看到了正确的方法,如 CreateTable()、Query() 等,但没有看到 CreateFile()。 Where is it?它在哪里? Or does the WinRT package use a different method for creating database files from code?或者 WinRT 包是否使用不同的方法从代码创建数据库文件?

var db = new SQLiteConnection(databasePath, SQLiteOpenFlags.Create | SQLiteOpenFlags.ReadWrite);

我是怎么做到的。

One of the times where Microsoft provides a straightforward example: Microsoft 提供了一个简单示例的时间之一:

https://docs.microsoft.com/en-us/windows/uwp/data-access/sqlite-databases https://docs.microsoft.com/en-us/windows/uwp/data-access/sqlite-databases

 await ApplicationData.Current.LocalFolder.CreateFileAsync("sqliteSample.db", CreationCollisionOption.OpenIfExists);
 string dbpath = Path.Combine(ApplicationData.Current.LocalFolder.Path, "sqliteSample.db");
 using (SqliteConnection db =
    new SqliteConnection($"Filename={dbpath}"))
{
    db.Open();

    String tableCommand = "CREATE TABLE IF NOT " +
        "EXISTS MyTable (Primary_Key INTEGER PRIMARY KEY, " +
        "Text_Entry NVARCHAR(2048) NULL)";

    SqliteCommand createTable = new SqliteCommand(tableCommand, db);

    createTable.ExecuteReader();

Because depending on how you add sqlite to a UWP project, not having createfile() available is still an issue in 2021.因为取决于您如何将 sqlite 添加到 UWP 项目,在 2021 年没有 createfile() 仍然是一个问题。

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

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