Saturday 25 December 2010

SQL in Android

Still flailing around a bit with this, but I found a useful looking example for how to create and access a database. After a bit of messing around I ended up with code like this:

        private static class OpenHelper extends SQLiteOpenHelper {
                OpenHelper(Context context) {
                        super(context, DATABASE_NAME, null, DATABASE_VERSION);
                }

                @Override
                public void onCreate(SQLiteDatabase db) {
                        String t, sql;
                        String idstring = "_ID integer primary key "+
                                          "autoincrement  not null  UNIQUE"
                        ;

                        sql     = "create table event ("
                                + "     " + idstring + ", "
                                + "     ev_name varchar not null"
                                + ")"
                        ;
                        db.execSQL(sql);


                        sql     = "create table event_string ("
                                + "     " + idstring
                                + "     , ev_name varchar not null"
                                + "     , stage integer not null"
                                + "     , string_id integer not null"
                                + ")"
                        ;
                        db.execSQL(sql);

                        sql     = "create table event_image ("
                                + "     " + idstring
                                + "     , ev_name varchar not null"
                                + "     , stage integer not null"
                                + "     , image_id integer not null"
                                + ")"
                        ;
                        db.execSQL(sql);




OK: so far, so good. I create the tables if the database itself needed creating, and I've decoupled the images and strings so any stage can have either, or both, (or none, although I'm not sure where I'd use that.

I'm less happy with the next bit...







/*
 *                      now create the database. This is the bit that
 *                      I'm not happy with. It's still hard-coding the
 *                      event structure into the program, which defeats
 *                      the point in many ways.
 *
 *                      What the hell - learning experience. I'll do it to
 *                      more forward and maybe revisit when I better understand
 *                      the platform
 */

                        insert_event("intro", R.string.intro1, R.drawable.alley);
                        insert_event("intro", R.string.intro2);
                        insert_event("intro", R.string.intro3);
                        insert_event("intro", R.string.intro4);
                        insert_event("intro", R.string.intro5);
                        insert_event("intro", R.string.intro6);
                        insert_event("intro", R.string.intro7);
                        insert_event("intro", R.string.intro8);
                        insert_event("intro", R.string.intro9, R.drawable.girl1);
                        insert_event("intro", R.string.intro10);
                        insert_event("intro", R.string.intro11);
                        insert_event("intro", R.string.intro12);


Apat from the fact that I'm not passing a stage number through (and apart from the fact that I really need to adopt a common nomenclature for all this stuff) The big problem here is that you can't look at the code and read the flow of the text. Sure you can do that in the strings.xml file, but you still get problems. If you add an entry to the XML, you still need to come back here and update the database creation. And there's no easy way to tell if the images are attached to the correct snippets.

What I want, really is to read from XML, and then load the database in a loop. That would mean I couldn't use resource strings; not a problem. I could still use image resource numbers, and I think I would do that, in fact. Eventually I need to think about loading girlpacks ... but that can wait. More important is getting a time management screen going

No comments: