作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个使用以下命令行连接到 Sqlite3 的 C++ 程序:
int ret = sqlite3_open("databasefilename", &dbHandler);
打开数据库后,我需要启用 PRIMARY_KEYS,就像我使用普通 SQL 所做的那样:
sqlite> PRAGMA foreign_keys = ON;
是否有允许我执行此操作的 C++ 单一命令?我知道在 PHP 上我们可以使用:
$service = new \Sqlite3($filename);
$service->enforceForeignKeys(true);
C++ 中有类似的东西吗?
最佳答案
In the C/C++ API for SQLite ,外键约束强制选项在您对 sqlite3_db_config()
的调用中指定:
sqlite3* db;
int err = sqlite3_open( "databaseFileName", &db );
if( err != SQLITE_OK ) /* die */
int fkeyConstraintsEnabled;
err = sqlite3_db_config( db, SQLITE_DBCONFIG_ENABLE_FKEY, /* either 0 or 1 to disable/enable constraints */, &fkeyConstraintsEnabled );
if( err != SQLITE_OK ) /* die */
printf( "Constraints now enabled: %d\r\n", fkeyConstraintsEnabled );
可以想象,您可以用 C/C++ 编写一个函数来包装它:
void enforceForeignKeys( sqlite3* db, bool enforceForeignKeyConstraints ) {
int expectedNewValue = enforceForeignKeyConstraints ? 1 : 0;
int actualNewValue;
int err = sqlite3_db_config( db, SQLITE_DBCONFIG_ENABLE_FKEY, expectedNewValue, &actualNewValue);
if( err != SQLITE_OK ) throw err;
if( actualNewValue != expectedNewValue ) throw SOME_USER_DEFINED_ERROR;
}
关于c++ - 如何以编程方式设置 Sqlite PRAGMA 选项(即 "primary_key = On")?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32342615/
我是一名优秀的程序员,十分优秀!