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;
}
來源
2015-09-01 23:59:48
Dai
完美的作品。謝謝... – Mendes