【问题标题】:Create new SQLite database on WinRT?在 WinRT 上创建新的 SQLite 数据库?
【发布时间】:2014-02-21 17:46:27
【问题描述】:
我想在我的 WinRT 应用程序中使用 C# 代码创建一个新数据库。我在网上搜索,这样做的方法似乎是 SQLiteConnection.CreateFile() 方法。但是,SQLite 命名空间中不存在该方法,至少在 WinRT 版本中是这样。我使用 NuGet 包名称“sqlite-net”安装了 SQLite,并将 sqlite3.dll 包含到我的项目中。我确实看到了像 CreateTable()、Query() 等正确的方法,但没有看到 CreateFile()。它在哪里?还是 WinRT 包使用不同的方法从代码创建数据库文件?
【问题讨论】:
标签:
c#
.net
sqlite
windows-runtime
【解决方案1】:
var db = new SQLiteConnection(databasePath, SQLiteOpenFlags.Create | SQLiteOpenFlags.ReadWrite);
我是这样做的。
【解决方案2】:
微软提供了一个简单的例子:
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();
因为根据您将 sqlite 添加到 UWP 项目的方式,没有可用的 createfile() 在 2021 年仍然是一个问题。