6

I want to force 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 ?

3
  • I don't have the code here, but a basic trigger worked for me on SQLite and (according to my unit tests) it worked on all Android versions. Commented Sep 21, 2010 at 20:24
  • do you think my code is correct or something is missing ? Commented Sep 21, 2010 at 21:09
  • Anyone looking for trigger syntax with implementation example in android. coderconsole.com/2015/02/android-sqlite-trigger-demo.html Commented Nov 26, 2016 at 7:13

5 Answers 5

6

Ok I got it

Android supports SQLite 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

Sign up to request clarification or add additional context in comments.

Comments

3

Foreign keys are only supported on Android on Froyo (2.2) or newer, for previous versions you can include them but SQLite ignores them. All Android versions of SQLite support triggers to produce the same effect though.

Newer versions of SQLite (for your PC) has a command called "genfkey" that will analyze your SQLite database (which has foreign keys in it) and produce the equivalent triggers. This way you can design your tables with foreign key constraints while also supporting all versions of the OS.

On Windows, open the SQLite command line tool with your database file as a parameter:

sqlite3 mydatabase.db
.genfkey --exec

This will generate triggers for all of your key constraints.

Comments

1

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.

2 Comments

Thanks Thomas I'll consider using it.
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.
1

To delete Last 50 rows when count is greater than 100

sqliteDB.execSQL("CREATE TRIGGER IF NOT EXISTS delete_trigger
    AFTER INSERT ON table1 
    WHEN (SELECT COUNT(*) FROM table1) > 50 " +
    BEGIN
       delete From table1 where id not in(select id from table1 order by id desc limit 100;
    END;"
);

Comments

0

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

3 Comments

thanks for your help, but can you provide me with any reference to this issue
I believe your assumption is wrong because it does support triggers
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: developer.android.com/reference/android/database/sqlite/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.