使用 sqlite pragma 语句来配置数据库?
我在项目中执行foreign_key pragma 语句时遇到问题。目前我已将其写入程序中。
SQLiteStatement pragma = db.compileStatement("PRAGMA foreign_key = ON;");
pragma.execute;
这些语句在创建数据库后执行,从那时起创建表,一切正常。
问题是,当我使用 sqlite3 调试数据库时,我在 sqlite3 中执行 (pragmaforeign_key;) 语句,它总是显示foreign_keys 已关闭,除非我在调试期间显式启用它。
所以我的问题是如何在我的应用程序中执行 PRAGMA 语句?
仅供参考,sqlite 版本是 3.6.22,默认情况下外键关闭,但没有定义省略外键或省略触发器。我知道我需要定义触发器来强制执行外键约束。虽然我确实想知道为什么。
I am having a problem executing the foreign_key pragma statement in my project. At the moment I have this written into the program.
SQLiteStatement pragma = db.compileStatement("PRAGMA foreign_key = ON;");
pragma.execute;
Those statements are executed after the db is created and from that point the tables are created, which all works fine.
The problem is when I debug the db using sqlite3 I execute the (pragma foreign_key;) statement in sqlite3 and it always shows that foreign_keys are off unless I explicitly enable it during debug.
So my question is how do I execute PRAGMA statements in my application?
just for reference the sqlite version is 3.6.22 which has foreign keys off by default but doesn't have the omit foreign key or omit trigger defined. and I am aware that I need to define triggers to enforce the foreign key constraints. Although I do wonder why.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
foreign_keys
pragma 设置会话变量;它不是数据库模式的一部分。它默认为每个“连接”关闭,如果要打开它,则需要为每个连接打开它。The
foreign_keys
pragma sets a session variable; it's not part of the database schema. It defaults to off for every "connection", and if you want to turn it on, you need to turn it on for each connection.