简体   繁体   中英

Create new SQLite database on WinRT?

I want to create a new database from C# code in my WinRT app. I searched the Web and the way to do that appears to be the SQLiteConnection.CreateFile() method. However, that method does not exist in the SQLite namespace, at least in the WinRT version. I installed SQLite using the NuGet packge name "sqlite-net" and included the sqlite3.dll into my project. I do see properly methods like CreateTable(), Query(), etc. but not CreateFile(). Where is it? Or does the WinRT package use a different method for creating database files from code?

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

我是怎么做到的。

One of the times where Microsoft provides a straightforward example:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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