简体   繁体   中英

How to set multiple PRAGMAs using SQLite.Net?

After opening a connection to the database, I can set one PRAGMA, but the second one always fails. How do I set these two PRAGMA's using SQLite.Net-PCL 3.1.1? This is for a Universal Windows Platform app.

    public static SQLiteConnection Open()
    {
        if (db == null)
        {
            var dbPath = Path.Combine(ApplicationData.Current.LocalFolder.Path, DatabaseFileName);
            db = new SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), dbPath);
            db.Execute("PRAGMA foreign_keys = ON");
            db.Execute("PRAGMA journal_mode = WAL");
        }
        return db;
    }

I get a "Row" exception from SQLite immediately after the second execute statement.

Requested Stack Trace:

  • at SQLite.Net.SQLiteCommand.ExecuteNonQuery()
  • at SQLite.Net.SQLiteConnection.Execute(String query, Object[] args)
  • at SceneLocker.Models.DBConnection.Open()

Edit: Adding a screenshot of the trapped exception:

被困的例外

I get a "Row" exception from SQLite immediately after the second execute statement.

The "Row" exception means the statements returns a row of result, which is not supported by db.Execute .

As a workaround, you can use db.CreateCommand to execute that statement:

var dbPath = Path.Combine(ApplicationData.Current.LocalFolder.Path, "MyDB.db");
db = new SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), dbPath);
db.Execute("PRAGMA journal_mode=DELETE");
var cmd=db.CreateCommand("PRAGMA journal_mode=WAL",new object[] { });//use db.CreateCommand
var result=cmd.ExecuteQuery<object>();//execute the command

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