SQLite triggers in Android? - android

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 ?

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

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.

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.

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;"
);

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

Related

Why does my Sqlite database get really slow when one table gets a lot of records?

I have an Sqlite database on Android and if I put 2000 records in one table all the other tables get really slow.
With 2000 records in the one table I can run a 'SELECT COUNT(*)' on a table with 0 records and it can take anywhere from 5 to 30 seconds.
There can be many reasons for that. The usual reasons are:
Poorly written query
Not using a primary key, assuming one even exists on the table
Poorly designed data model (table structure)
Lack of indexes
IMO, your case may be lack of indexes. Check if you have indexed your tables/rows.
"SELECT COUNT( * )", the char ' * ' need database to extract every field when sql execute.
As an additional suggest, you can use primary key in the " count( < your primary key > ) ".

SQLiteLog Accessing the log or piping it out [Android]

Is there a way to access the SQLiteLog or at least pipe out errors to do with SQLite?
I would like to automatically send any errors, like the below, so I can optimise the database by adding in indexes, or making other changes as need be.
E/SQLiteLog: (284) automatic index on messages(chat_id)
It could well be that I'd only want to catch code 284, rather than getting everything. Is there a way of doing this when building the app, as would be useful to pass to crashlytics to help with development going forward
A potential alternative, would be to run the queries preceded with EXPLAIN QUERY PLAN, and to then check for AUTOMATIC COVERING INDEX in the results.
e.g.
DROP TABLE IF EXISTS table1;
DROP TABLE IF EXISTS table2;
CREATE TABLE IF NOT EXISTS table1 (column1 TEXT, column2 TEXT);
CREATE TABLE IF NOT EXISTS table2 (column3, column4);
EXPLAIN QUERY PLAN
SELECT * FROM table1, table2 WHERE column1=column3;
Results in :-
As you can see this is based upon a predication of the number of rows rather than the actual number of rows, so works if there is no data (as above), which could well be beneficial, from a development perspective, as opposed to trying to trap on a as happens basis.

Android 2.2 + SQLite + Defining referential integrity constraints

I'm developing an Android 2.2+ app with SQLite db support. I'm trying to define referential integrity constraints while creating the tables. I've also enabled the foreign key support by executing db.execSQL( "PRAGMA foreign_keys=ON;" ); in onCreate and onOpen methods of SQLiteOpenHelper. Afteer setting up my tables and proper foreign key references, it still allows me to insert rows in tables where reference records are missing. For e.g. I've following structure
CREATE TABLE Questions(_id integer primary key,question text not null);
CREATE TABLE Ques_Ans(_id integer primary key autoincrement,qid integer not null,
aid integer not null,is_correct integer default 0,
FOREIGN KEY (qid) REFERENCES Questions(_id));
and following my data in the table
INSERT INTO Questions VALUES(1, 'Some text');
INSERT INTO Ques_Ans(qid, aid, is_correct) VALUES(20, 1, 0);
If foreign key is properly set on Ques_Ans table, 2nd insert should have failed as there is no record in Questions table with id 20, but somehow my app does not thrown any error and inserts the 2nd insert statement. Can anybody tell me whats wrong over here or am I missing any configuration over here?
Update [03-Mar-2012]: After discussion thread with #Catcall
Using sqlite3 tool, switching on PRAGMA foreign_keys=ON; foreign key works as expected
Same thing if used via app on Emulator or on Phone does not work
Insert statements executed using insert() or execSQL(). None of them throw foreign key constraint failed error
PRAGMA integrity_check returns ok. So database is not corrupt.
Your foreign key is on the column "qid", not on the column "_id".
This INSERT statement
INSERT INTO Ques_Ans VALUES(20, 1, 0);
should have thrown the error
Error: table Ques_Ans has 4 columns but 3 values were supplied
This INSERT should succeed.
INSERT INTO Ques_Ans VALUES(20, 1, 0, 0);
This one should fail.
INSERT INTO Ques_Ans VALUES(21, 2, 0, 0);
Error: foreign key constraint failed
Later . . .
Since it works in sqlite, but not in your emulator, the problem is probably in either the emulator or your code.
This is the idiom for database transactions.
db.beginTransaction();
try {
...
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
Android docs suggest using insert() instead of execSQL(). Make sure you're trapping, catching, or checking for errors in every function that can return an error. So, for example, if you switch to insert(), check its return value for -1.
If that doesn't help, you might try tagging this question with "Java" (for example), or asking a new question that focuses on your code and on catching errors.
IN SQLite Foreign key constraints are disabled by default (for backwards compatibility). You have to enable it explicitly using
PRAGMA foreign_keys = 1
after you establishing your connection with the database. Here's the link to the official docs that explains it in more depth. http://sqlite.org/foreignkeys.html Please navigate to enabling foreign key support in the above link.

data duplication in sqlite with android app

i am getting information from user in sqlite database.
But when i insert same record which is already in database it is added again.
how i can stop duplication of record in sqlite. I am developing this in android.
I am using mobile number as primary key. still it add that record in database.
Please suggest me appropriate solutions.
Thanks in advanced.
Be aware of the limitations of REPLACE or INSERT OR REPLACE as these will overwrite any custom data your app user has added to these rows in the database - it is not as advanced as UPSERT in other SQL databases.
As mentioned in a previous post you really need to identify what the primary key could be and use this information to either update old data or to remove an old row before inserting the fresh one.
If this is not possible then you could always DELETE FROM my_table or DROP my_table before running the insertions so that there will be no duplicates. This will (for better or worse) also make sure that data that is missing from new imports is not left lying around in your app.
make sure you have set your phone number as Primary Key at the time you created the table.
for example:
String query = "CREATE TABLE IF NOT EXISTS PhoneBook ("+
"TelNum VARCHAR(100) PRIMARY KEY NOT NULL,"+
"Address TEXT);";
db.execSQL(query);
and in case you want to enforce foreign keys defined in your table then call the following method before doing anything in your database
db.execSQL("PRAGMA foreign_keys = ON;"); //enforcing FK
Use REPLACE INTO keyword:
REPLACE INTO my_table (pk_id, col1) VALUES (5, '123');
This automatically identifies the primary key and finds a matching row to update, inserting a new one if none is found.

Android SQLite, some SQL basics

I am following this tutorial: http://www.codeproject.com/KB/android/AndroidSQLite.aspx
I must be overthinking this SQLite stuff (in the past my domain server would automatically initialize databases I requested, and I could do queries when desired. never put one together from scratch)
I have some questions about their onCreate function. I never recall using a
CREATE TRIGGER command in my SQL
I only need to create one table with 2 or 3 columns (if you count the primary key)
I should just be able to do
db.execSQL("CREATE TABLE" + tableName +"("+colID+"INTEGER PRIMARY KEY,"+columnName+"TEXT)");
correct?
Do I need a "Trigger" and a "View" ?
If you just need a place to store some data - then Table is enough. But if your logic is more complicated then you'll need additional stuff. Also note that some Triggers are not supported by SQLite: Info from here
You not need to create TRIGGER. Unless it is required. Here is how I implemented in one of my project. Hope this help.
https://github.com/gopalB/FeedReader/blob/master/src/com/feedReader/provider/FeedDB.java
If you do not need a Trigger or a View, then you do not need to create them. It appears that the tutorial is just explaining some of the things you can do.
if SQLite TRIGGER and VIEW are similar to what they're used for in MySQL then no, they are not necessarily for what you're trying to accomplish.
VIEWs are useful when you have complex queries (like when using JOINs to join data from multiple tables).
TRIGGERSs are conditions that are run when you modify a table. (like using UPDATE, or INSERT)
As written, your create statement won't work because of a lack of whitespace. Try:
db.execSQL("CREATE TABLE " + tableName +" (" + colID + " INTEGER PRIMARY KEY, " + columnName + " TEXT)");

Categories

Resources