Android Tutorial

(avery) #1
Android Tutorial 319

public void onCreate(SQLiteDatabase db) {
db.execSQL(“CREATE TABLE “ +PetType.PETTYPE_TABLE_NAME+” (“



  • PetType._ID + “ INTEGER PRIMARY KEY AUTOINCREMENT ,”

  • PetType.PET_TYPE_NAME + “ TEXT”

  • “);”);
    db.execSQL(“CREATE TABLE “ + Pets.PETS_TABLE_NAME + “ (“

  • Pets._ID + “ INTEGER PRIMARY KEY AUTOINCREMENT ,”

  • Pets.PET_NAME + “ TEXT,”

  • Pets.PET_TYPE_ID + “ INTEGER” // FK to pet type table

  • “);”);
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion,
    int newVersion){
    // Housekeeping here.
    // Implement how “move” your application data
    // during an upgrade of schema versions
    // Move or delete data as required. Your call.
    }
    @Override
    public void onOpen(SQLiteDatabase db) {
    super.onOpen(db);
    }
    }


Now we can create a member variable for our database like this:

PetTrackerDatabaseHelper mDatabase = new
PetTrackerDatabaseHelper(this.getApplicationContext());


Now, whenever our application needs to interact with its
database,we request a valid database object. We can request a
read-only database or a database that we can also write to. We can
also close the database. For example, here we get a database we
can write data to:

SQLiteDatabase db = mDatabase.getWritableDatabase();
Free download pdf