dimanche 7 février 2016

sqlite: how to enable foreign keys in ios (PRAGMA foreign_keys always returns 0)

IOS, SQLite Implementation Question

Despite attempting to turn foreign keys on immediately after establishing a connection, and even though the result of that query does not produce an error, my subsequent check reveals that foreign keys are not enabled.

I can't figure out why. Here is the code:

    var sqliteDB: COpaquePointer = nil
    let dbPath = (documentsPath as NSString).stringByAppendingPathComponent("test.sqlite3")

    let status = sqlite3_open(dbPath.cStringUsingEncoding(NSUTF8StringEncoding)!, &sqliteDB)
    if status != SQLITE_OK {
        print("Error Opening Database")
        if let errMsg = String.fromCString(sqlite3_errmsg(sqliteDB)) {
            print("\(errMsg)")
        }
    }

    if sqlite3_exec(sqliteDB, "PRAGMA foreign_keys = ON", nil, nil, nil) != SQLITE_OK {
        let err = String.fromCString(sqlite3_errmsg(sqliteDB))
        print("error attempting to enable foreign keys: \(err)")
    }

    let test = sqlite3_exec(sqliteDB, "PRAGMA foreign_keys", nil, nil, nil)

    print("Result of check: \(test)")   // Result of check: 0

In the above code 'test' is always zero, indicating that foreign keys are not enabled. I have also tried using sqlite3_prepare_v2.

Any help would be greatly appreciated.

Aucun commentaire:

Enregistrer un commentaire