Wednesday, April 20, 2011

Enabling Foreign Keys in sqlite

Once again I must learn to read documentation carefully.

I was perusing the sqlite documentation on implementing foreign keys and created my tables in the way they said to and it wasn't working when I tried to create a row that violated my key constraint.  After a lot of teeth gnashing I finally found a small paragraph in the documentation.  

QUOTED:

Enabling Foreign Key Support

In order to use foreign key constraints in SQLite, the library must be compiled with neither SQLITE_OMIT_FOREIGN_KEY or SQLITE_OMIT_TRIGGER defined. If SQLITE_OMIT_TRIGGER is defined but SQLITE_OMIT_FOREIGN_KEY is not, then SQLite behaves as it did prior to version 3.6.19 - foreign key definitions are parsed and may be queried using PRAGMA foreign_key_list, but foreign key constraints are not enforced. The PRAGMA foreign_keys command is a no-op in this configuration. If OMIT_FOREIGN_KEY is defined, then foreign key definitions cannot even be parsed (attempting to specify a foreign key definition is a syntax error).
Assuming the library is compiled with foreign key constraints enabled, it must still be enabled by the application at runtime, using the PRAGMA foreign_keys command. For example:
sqlite> PRAGMA foreign_keys = ON;
Foreign key constraints are disabled by default (for backwards compatibility), so must be enabled separately for each database connection separately. (Note, however, that future releases of SQLite might change so that foreign key constraints enabled by default. Careful developers will not make any assumptions about whether or not foreign keys are enabled by default but will instead enable or disable them as necessary.) The application can can also use a PRAGMA foreign_keys statement to determine if foreign keys are currently enabled. The following command-line session demonstrates this:
sqlite> PRAGMA foreign_keys;
0
sqlite> PRAGMA foreign_keys = ON;
sqlite> PRAGMA foreign_keys;
1
sqlite> PRAGMA foreign_keys = OFF;
sqlite> PRAGMA foreign_keys;
0
Tip: If the command "PRAGMA foreign_keys" returns no data instead of a single row containing "0" or "1", then the version of SQLite you are using does not support foreign keys (either because it is older than 3.6.19 or because it was compiled with SQLITE_OMIT_FOREIGN_KEY or SQLITE_OMIT_TRIGGER defined).
It is not possible to enable or disable foreign key constraints in the middle of a multi-statement transaction (when SQLite is not in autocommit mode). Attempting to do so does not return an error; it simply has no effect.
It seems I have to turn it on or off..

DOH..  Hopefully this blog entry will help someone.

Monday, April 18, 2011

Android Database Tutorial I found on the net

This is an extremely useful Android database tutorial I found on the net at Another Android Blog.  I had been trying to wrap my head around an easy way to use sqlite in Android and this really put me over the top.  Show the guy some love and send him a donation if it helps you.

Tuesday, April 12, 2011

Android in action, second edition

So I have this Safari Online account through work.  As I get more and more into my Android development hobby I find my self referring to this book "Android in Action" a lot.  I just thought I'd share it with the rest of whoever reads this blog.

10 useful solutions for Android developers courtesy of Shaman.sir

Here's a very useful article for Android developers courtesy of Shaman.sir

Monday, April 4, 2011

Video on Android UI patterns

Here's a video I stumbled soon on ui patterns from Google I/O. Its caused me to rethink a lot of my impressions.

Sunday, April 3, 2011

Android project #2: My Savings

I think I've come up with my next project. My wife is a big time coupon clipper so to help her I'm planning to develop some sort of tracker for couponing. I think initially I'm going to start with calculating savings and then moving on from there. I might even add a bar code reader into it.