This is a Java method that will create a SQLite foreign key constraint for
inserts or updates.  You pass in the action (Insert, Update), the table
name, the foreign key field name, the foreign table name, and the foreign
table key field name.


        public String createForeignKeyConstraint(String action, String table,
String foreignKey, String foriegnTable,
                        String foreignField){
                StringBuilder sb = new StringBuilder();
                sb.append("CREATE TRIGGER
").append(table).append("_FK_").append(foreignKey).append("_").append(action).append("
");
                sb.append("BEFORE ").append(action).append(" ON 
").append(table).append("
");
                sb.append("FOR EACH ROW BEGIN SELECT RAISE(ROLLBACK, ");
                sb.append("'").append(action).append(" on table 
").append(table);
                sb.append(" violates foreign key constraint for column:
").append(foreignKey).append("') ");
                sb.append("WHERE  NEW.").append(foreignKey).append(" IS NOT 
NULL ");
                sb.append("AND (SELECT ").append(foreignField).append(" FROM
").append(foriegnTable).append(" ");
                sb.append("WHERE ").append(foreignField).append(" =
NEW.").append(foreignKey).append(") IS NULL; END; ");
                
                return sb.toString();
        }

-- 
View this message in context: 
http://www.nabble.com/Java-Code-to-Generate-Foreign-Key-Constraint-Trigger-tp24779328p24779328.html
Sent from the SQLite mailing list archive at Nabble.com.

_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to