It appears that SQLite does not enforce foreign keys by default. I’m using sqlitejdbc-v056.jar and I’ve read that using PRAGMA foreign_keys = ON; will turn on foreign key constraints, and that this needs to be turned on in a per-connection basis.
My question is: what Java statements do I need to execute to turn on this command? I’ve tried:
connection.createStatement().execute("PRAGMA foreign_keys = ON");
and
Properties properties = new Properties();
properties.setProperty("PRAGMA foreign_keys", "ON");
connection = DriverManager.getConnection("jdbc:sqlite:test.db", properties);
and
connection = DriverManager.getConnection("jdbc:sqlite:test.db;foreign keys=true;");
but none of those work. Is there something I am missing here?
I’ve seen this answer and I want to do exactly the same thing, only using JDBC.
When you look at the SQLite Foreign Key Support page I would interpret that
Ad 1) Quoted from here:
What is your result for
PRAGMA foreign_keys;?Update: from your comment I see you are using 3.6.14.2, this means your version is not supporting foreign key constraints! So you have to update SQLite, if this is possible!
Ad 2) Your first code snippet executes the PRAGMA as statement, I don’t think this will work. The third snipped didn’t work based on your comment: the SQLite driver interprets the whole string as the location of the database, instead of taking the “foreign keys=true” part as connection settings”. So only the second snippet will work.
Ad 3) Did you create the table with foreign key support? Quoted from here: