Mobile Software Development ISCG 7424 Department of Computing UNITEC John Casey and Richard Rabeder SQLite and Permissions
Content 2 SQLite Helper classes Gotchas Permissions Readings
SQLite 3 Small,fast,compact, embedded SQL database Not a full featured SQL dialect Every Android phone has a copy of SQLite No database administration Need to write Java code to create and alter table structures
SQLite 4 One file per database Locally available persistent data store – can access information when not connected to the internet / network DDL and DML Can perform CRUD (create, read, update, delete) Insert Select Update Delete Using RAW SQL interface or SQLite helper methods
SQLite Application Structure 5 Extend SQLiteOpenHelper and override OnCreate() OnUpgrade() Define database fields, table name and database version Declare helper subclass in Activity object Open Readable or Writable database as needed
Declare Constants 6 public final static int VERSION = 1; public final static String NAME = "mobile.db"; public final static String TABLE = "CLIENT"; public final static String CID ="CID"; public final static String FIRSTNAME="FIRSTNAME"; public final static String LASTNAME="LASTNAME"; public final static String NOTES="NOTES";
7 public class W5DBHelper extends SQLiteOpenHelper { public final static int VERSION = 1; //... this is where the constants from previous page would go public W5DBHelper(Context context) { super(context, NAME, null, VERSION); public void onCreate(SQLiteDatabase db) { String sql = "create table " +TABLE +" ("+CID+" integer primary key, "+FIRSTNAME+" text, "+LASTNAME+" text, "+NOTES+" text);"; db.execSQL(sql); public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("drop table if exists "+TABLE); onCreate(db); } Define Helper
Using the Helper class 8 W5DBHelper helper = new W5DBHelper(getApplicationContext()); dbHandler = helper.getWritableDatabase(); String sqlInsert = "insert into CLIENT (CID,FIRSTNAME, LASTNAME, NOTES) values(null,'John','Casey','blah blah blah');"; dbHandler.execSQL(sqlInsert);
SQLite Helper Methods 9 ContentValues values = new ContentValues(); values.putNull(W5DBHelper.CID); values.put(W5DBHelper.FIRSTNAME,"John"); values.put(W5DBHelper.LASTNAME,"Casey"); values.put(W5DBHelper.NOTES,"Blah blah”); dbHandler.insert(W5DBHelper.TABLE,null, values);
Things to remember SQLite isn’t Oracle or MS SQL Server Has its own very unique syntax Android’s SQLite appears to be slightly different to plain old SQLite Does not completely implement all the SQL features you would normally expect: data types, stored procs, even some types of join query
Permissions 11 Per resource model: Grant / Revoke Location Phone calls Blue tooth Network access Wi-fi Internet Personal information Spend money on the market Its easy!
Permissions 12 Declare resources that the application needs in the manifest file – before the application XML block Application will then prompt user to grant that resource to the application
Readings 13 ml ml /sqlite/package-summary.html /sqlite/package-summary.html sqlite/ sqlite/ example-using-androids-sqlite.html example-using-androids-sqlite.html