看來,SQLite默認情況下不強制執行外鍵。我正在使用sqlitejdbc-v056.jar,我讀過使用PRAGMA foreign_keys = ON;
將打開外鍵約束,並且需要在每個連接的基礎上打開它。如何通過Java在SQLite中實施外鍵約束?
我的問題是:我需要執行哪些Java語句來打開此命令?我已經試過:
connection.createStatement().execute("PRAGMA foreign_keys = ON");
和
Properties properties = new Properties();
properties.setProperty("PRAGMA foreign_keys", "ON");
connection = DriverManager.getConnection("jdbc:sqlite:test.db", properties);
和
connection = DriverManager.getConnection("jdbc:sqlite:test.db;foreign keys=true;");
但這些都不工作。有什麼我在這裏失蹤?
我見過this answer我只想用JDBC來做同樣的事情。
你正在使用什麼版本的SQLite? (在3.6.19中引入了外鍵約束。) – dan04 2012-03-22 23:18:54
3.6.14.2,顯然。甚至沒有意識到。 – Zarjio 2012-03-22 23:25:30