Android Database Tutorial – Active Android

posted in: Android, Database, Tutorial | 4

In this Android database tutorial, we’ll be using Active Android for our ORM, or object relational mapping to convert our database data into plain old Java objects (POJOs). This ORM saves us from having to write SQLite code, which is error prone since it’s not checked by the compiler. These are referred to as runtime errors.Android Database Tutorial



In the video below, we show you how to save data between application launches, or in other words, persist that data. We’ll also show you how to retrieve, update and delete the data. We’ll also show you how to link to other objects that are also stored a different database table.
Now, you could do this manually, using Android’s SQLiteOpenHelper class, or you can abstract that functionality and use what’s called an ORM or Object-relational mapping which is a design pattern for accessing a relational database from our Android code as plain old Java objects. Each ORM is different, so you should choose the best one suited for your particular application and tastes. Even though we’ll be showing you Active Android, many of these concepts apply to other ORMs. Each have their advantages and disadvantages.
Active Android is a very powerful tool as you don’t have to write a single line of SQL code, which is error prone due to the fact that you have to write and construct long SQL commands as strings, where error can’t be detected by the compiler. Instead, you simple mark up your Java object model using Java annotations, extend that Java class with the Model class, and you simply save that model’s data to the database by calling the save method on that object. To update the data, just update that same object, and call save of it again. Active Android will determine if the record is already in the database. If so, it’ll perform an update. If not, it’ll perform an insert operation. To learn more about databases, I have a previous video on databases that you should watch.

Android Database Tutorial

 

 Example Code




4 Responses

  1. Aparna

    Hey very nice tutorial. Explained in a very easy way!
    Is it possible to fetch values and display them in an activity? I want to store the values address, body, date and time in db. Fetch and display them
    Here is a code snippet
    public ArrayList fetchInbox() {
    ArrayList sms = new ArrayList();
    ArrayList sms_num = new ArrayList();
    String value = “address”;
    Uri uriSms = Uri.parse(“content://sms/inbox”);
    Cursor cursor = getContentResolver().query(uriSms, new String[]{value, null, “date desc”, “_id”, “address”, “date”, “body”}, null, null, null);

    cursor.moveToFirst();

    while (cursor.moveToNext()) {
    String address = cursor.getString(3);
    Pattern p = Pattern.compile(“[A-Z]{2}[-]{1}[A-Z]|[a-z]{6}”);
    Matcher m = p.matcher(address);
    if (m.find()) {

    String body = cursor.getString(5);
    String time = cursor.getString(1);
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(Long.parseLong(time));
    time = time.format(String.valueOf(calendar.getTime()));

    System.out.println(“======> Mobile number => ” + address);
    System.out.println(“=====> SMS Text => ” + body);

    sms.add(“Sender= ” + address + “\n” + “SMS = ” + body + ” \nMessage Date & Time=” + time);
    }
    }

    return sms;
    }

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.