CREATE TABLE track( trackid INTEGER,trackname TEXT,trackartist INTEGER,FOREIGN KEY(trackartist) REFERENCES artist(artistid)@H_301_8@ );
CREATE TABLE track( trackid INTEGER,trackname TEXT,trackartist INTEGER,FOREIGN KEY(trackartist) REFERENCES artist(artistid)@H_301_8@ );
NSString* dbPath = [(NSArray*)NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; dbPath = [dbPath stringByAppendingPathComponent:@"test.db"]; db = [FMDatabase databaseWithPath:dbPath]; if ([db open]) { NSLog(@"Database %@ opened", dbPath); //check for foreign_key NSString* sql = @"PRAGMA foreign_keys"; FMResultSet *rs = [db executeQuery:sql]; int enabled; if ([rs next]) { enabled = [rs intForColumnIndex:0]; } [rs close]; if (!enabled) { // enable foreign_key sql = @"PRAGMA foreign_keys = ON;"; [db executeUpdate:sql]; // check if successful sql = @"PRAGMA foreign_keys"; FMResultSet *rs = [db executeQuery:sql]; if ([rs next]) { enabled = [rs intForColumnIndex:0]; } [rs close]; } // do your stuff here,or just cache the connection } else { NSLog(@"Failed to open %@", dbPath);@H_301_8@ @H_371_403@}
table1 is the parent table having id1 as primary key. CREATE TABLE "table1" ("id1" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL) table2 is the child table having id2 as a foreign key with reference to id1 of table1. CREATE TABLE table2 ( id2 INTEGER, parent_id INTEGER, description TEXT, FOREIGN KEY (id2) REFERENCES table1(id1)@H_301_8@ )