tags:

views:

149

answers:

3

Hi all

I want to forece a foreign key constarint on a table in an Android application.

I've searched that this can be done by using triggers:

I did it like this:

db.execSQL("CREATE TRIGGER dept_id_trigger22+" +
                " AFTER INSERT "+
                " OF EmployeeName ON Employees"+
                " BEGIN"+
                                     //Condition
                " RAISE(ABORT,'error') END;");

but no error was raised and the illegal values are inserted.

what is wrong with this ?

A: 

I discovered that the SQLite version used does not support foreign keys - so I expect that triggers are not supported, too.

mreichelt
thanks for your help, but can you provide me with any reference to this issue
Mina Samy
I believe your assumption is wrong because it does support triggers
Mina Samy
It seems so. I searched for it, and from what I read I think that these features are available, but not activated by default. I don't know how to use triggers yet, but there is an interesting method in the class SQLiteDatabase called markTableSyncable (String table, String foreignKey, String updateTable) which maybe is what you want: http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#markTableSyncable%28java.lang.String,%20java.lang.String,%20java.lang.String%29
mreichelt
A: 

I don't expect any votes for this answer, just to let you know:

You could use another database, for example the H2 database. Disclaimer: I'm the main author of H2.

There are some disadvantages: some (not all) operations are slower, for example opening and closing a database. The jar file is relatively big (about 1 MB). You would have to use the JDBC API.

But the advantage is: H2 supports using triggers, constraints, and so on.

Thomas Mueller
Thanks Thomas I'll consider using it.
Mina Samy
I should probably add: According to my (limited) testing some operations are faster and some are a lot slower. I'm working on that, and you can expect that performance of H2 on Android will improve in the next months. I also want to implement the Android APIs so that switching from SQLite to H2 is easier.
Thomas Mueller
A: 

Ok I got it

SQLite on Androi supports triggers the correct syntax is

db.execSQL("CREATE TRIGGER dept_id_trigger22" + 
                " AFTER INSERT "+ 
                "ON Employees"+ 
                " BEGIN"+ 
                                     //Condition 
                " SELECT RAISE(ABORT,'error'); END;"); 

I forgot to add semicolon after the raise statement.

this does not execute the statement but it does not throw an exception. still will search for how to throw exceptions

thanks

Mina Samy