Monday, February 21, 2011

Android database

Android has SQLite3 built in. and its got a strange way to do JDBC-like database.

You need to implement the SQLiteOpenHelper to define your schema with CREATE statements in the onCreate on provide the destruction statements with onUpgrade. You then get the db instance with the getWritableDatabase()... and may call the methods to query.

Look at how they do query. Instead of SELECT statement, you do this:


public Cursor query (String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)


Don't be surprised if you pass in a bunch of null, null and nulls. Ok, for those who prefers SELECT statement, you can do a rawQuery.

Instead of UPDATE statement, you do this:


public int update (String table, ContentValues values, String whereClause, String[] whereArgs)


Why abandon the goodness of tried and true SQL?

So this is a new approach to DB, different from JDBC... and SOMEBODY is going to say this is not cool an do XML wrapping on things!
* * *

Being to do little database in a device is pretty cool. You want to mess with little memory blocks instead?

Besides SQLite, you can also write files or deal with the memory card, or good old value pairs for preferences.

Whoa, their is an linux shell you can deal with directly: adb shell.
See here for glorious details.

Graphics, database... http calls, and even geomap are just the beginning. Still many interesting things on the Android... There are all kinds of sensors that takes programming to a new level of possibilities.

No comments: