CREATE TABLE track( trackid INTEGER,trackname TEXT,trackartist INTEGER,FOREIGN KEY(trackartist) REFERENCES artist(artistid)
);
打开外键支持
PRAGMA foreign_keys = ON;
这一端是拷贝的别人的,不过经过我测试发现,不需要这么麻烦的,只需要执行一句就可以了
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);
}
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)
)
在网上看到的一段对于外键的说明:
http://s6453.socode.info/question/5081350e4f1eba38a42cbbbc
Foreign keys are disabled by default. You have to enable them separately for each connection. The setting isn't "sticky". You have to do this every time you connect to a sqlite database. PRAGMA foreign_keys = ON;
在它下面看到
CREATE TABLE artist(
artistid INTEGER PRIMARY KEY,
artistname TEXT
)
CREATE TABLE "track" (
"trackid" INTEGER PRIMARY KEY AUTOINCREMENT,
"trackname" TEXT,
"trackartist" INTEGER,
CONSTRAINT "trackartist" FOREIGN KEY ("trackartist") REFERENCES "artist" ("artistid") ON DELETE CASCADE ON UPDATE CASCADE)
作者也使用的是FMDB这个库
使用的方式如下:
[db
executeUpdate
:[
Nsstring
stringWithFormat
:
@"create table provincecity (valuekey text PRIMARY KEY,value text,province text,CONSTRAINT province FOREIGN KEY(province) REFERENCES province(valuekey) )"
]];
删除使用的是
[db
executeUpdate
:[
Nsstring
stringWithFormat
:
@"DELETE FROM province WHERE valuekey=10102000"
]];
2013-10-31 11:59:25.384 testDatabase[23225:70b] DB Query: DELETE FROM province WHERE valuekey=10102000
2013-10-31 11:59:25.384 testDatabase[23225:70b] UnkNown error finalizing or resetting statement (19: foreign key constraint Failed)
2013-10-31 11:59:25.385 testDatabase[23225:70b] DB Query: DELETE FROM province WHERE valuekey=10102000
[db
executeUpdate
:
@"PRAGMA foreign_keys=ON;
”
];
但是问题依旧......
那文章最后说,要把删除的语句 换成使用
executeQuery,即
[db
executeQuery
:[
Nsstring
stringWithFormat
:
@"DELETE FROM province WHERE valuekey=10102000"
]];
看到上面那段,抱着试试看的心理,进行同样的设置
[db
executeUpdate
:[
Nsstring
stringWithFormat
:
@"create table provincecity (valuekey text PRIMARY KEY,CONSTRAINT province FOREIGN KEY(province) REFERENCES province(valuekey) ON DELETE CASCADE ON UPDATE CASCADE)"
]];
再执行
哈哈,这下成功了
原文地址:https://www.jb51.cc/sqlite/200790.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。