如何使用 SQLitePCL 设置 'PRAGMA foreign_keys = ON' 语句
How to set the 'PRAGMA foreign_keys = ON' statement with SQLitePCL
我一直在使用SQLitePCL (currently ver 3.8.7.2), and now decided to experiment with Delete and Update Cascade by turning on the Foreign Key constraint. I understand this feature is disabled by default and according to SQLite documentation,必须为每个数据库连接单独启用约束。
连接字符串只采用数据库路径(无论如何对于 SQLitePCL)并且不允许 data source=d:\foo\bar\mySqlite.db;foreign keys=ON
形式的更灵活的复合连接字符串。如果我必须为每个连接打开约束,如下所示,如何打开约束?
我期待 ISQLiteStatement
API 提供一些将 PRAGMA foreign_keys = ON
语句注入到我的 连接语句 中的方法,但没有明显的发现[Intellisense] 方法或 属性 来实现这一点。探索 SQLiteConnection API 甚至不是一个开始,因为打开外键约束是 每个连接 无论如何。
注意:下面的 DeleteItemByIdQuery()
和 BindIdToDeleteItemByIdQuery()
方法 return SQL 为简洁起见省略了查询字符串和详细信息。
using (ISQLiteStatement statement = new SQLiteConnection("d:\foo\bar\mySqlite.db").Prepare(DeleteItemByIdQuery()))
{
BindIdToDeleteItemByIdQuery(statement, id);
SQLiteResult result = statement.Step();
}
我是忽略了一些简单的事情,还是这不可能?求助!
PRAGMA语句是一个普通的SQL语句;直接执行即可。
为了减少输入量,您可以编写一个辅助函数来创建连接并进行配置。
好的,我明白了:
将 SQLiteConnection
保留为外部 using
块,并在其中执行一系列 PRAGMA 和主语句。有趣的是,我以为我以前尝试过同样的方法,但没有得到现在得到的结果——当时可能还有其他错误。
using (var conn = new SQLiteConnection(@"d:\foo\bar\mySqlite.db"))
{
// First turn ON the FK constraint
using (var statement = conn.Prepare(@"PRAGMA foreign_keys = ON"))
{
SQLiteResult result = statement.Step();
}
// Then Delete item that will Cascade deletion in referencing table(s)
using (var statement = conn.Prepare(SqlDeleteItemById()))
{
SqlBindIdToDeleteItemById(statement, id);
SQLiteResult result = statement.Step();
}
}
我一直在使用SQLitePCL (currently ver 3.8.7.2), and now decided to experiment with Delete and Update Cascade by turning on the Foreign Key constraint. I understand this feature is disabled by default and according to SQLite documentation,必须为每个数据库连接单独启用约束。
连接字符串只采用数据库路径(无论如何对于 SQLitePCL)并且不允许 data source=d:\foo\bar\mySqlite.db;foreign keys=ON
形式的更灵活的复合连接字符串。如果我必须为每个连接打开约束,如下所示,如何打开约束?
我期待 ISQLiteStatement
API 提供一些将 PRAGMA foreign_keys = ON
语句注入到我的 连接语句 中的方法,但没有明显的发现[Intellisense] 方法或 属性 来实现这一点。探索 SQLiteConnection API 甚至不是一个开始,因为打开外键约束是 每个连接 无论如何。
注意:下面的 DeleteItemByIdQuery()
和 BindIdToDeleteItemByIdQuery()
方法 return SQL 为简洁起见省略了查询字符串和详细信息。
using (ISQLiteStatement statement = new SQLiteConnection("d:\foo\bar\mySqlite.db").Prepare(DeleteItemByIdQuery()))
{
BindIdToDeleteItemByIdQuery(statement, id);
SQLiteResult result = statement.Step();
}
我是忽略了一些简单的事情,还是这不可能?求助!
PRAGMA语句是一个普通的SQL语句;直接执行即可。
为了减少输入量,您可以编写一个辅助函数来创建连接并进行配置。
好的,我明白了:
将 SQLiteConnection
保留为外部 using
块,并在其中执行一系列 PRAGMA 和主语句。有趣的是,我以为我以前尝试过同样的方法,但没有得到现在得到的结果——当时可能还有其他错误。
using (var conn = new SQLiteConnection(@"d:\foo\bar\mySqlite.db"))
{
// First turn ON the FK constraint
using (var statement = conn.Prepare(@"PRAGMA foreign_keys = ON"))
{
SQLiteResult result = statement.Step();
}
// Then Delete item that will Cascade deletion in referencing table(s)
using (var statement = conn.Prepare(SqlDeleteItemById()))
{
SqlBindIdToDeleteItemById(statement, id);
SQLiteResult result = statement.Step();
}
}