Log in

I forgot my password

Poll

What size screen do you think is best for the next device?
12% 12% [ 6 ]
23% 23% [ 12 ]
21% 21% [ 11 ]
13% 13% [ 7 ]
31% 31% [ 16 ]

Total Votes : 52

Latest topics

» Rebuild gator driver to open mali option
Today at 3:06 pm by ilgwt

» Memory, final code and final video (in Spanish)
Today at 9:05 am by Alvaro

» ODROID-A4 IO Board
Today at 6:36 am by tuxfield

» odroid linux kernel config file "hkdk_rtm20_defconfig " missed
Yesterday at 10:09 am by odroid

» odroid-7 source code
Sun May 20, 2012 7:52 am by odroid

» Application source code for Embedded Android Platform based on ODROID-A4
Sun May 20, 2012 2:52 am by odroid

» What is ODROID-BaB project LSED?
Sat May 19, 2012 9:44 am by Alvaro

» CPU Module S5PV310 and K3PE7E700B
Fri May 18, 2012 6:01 am by odroid

» BF040-I50B_N09 Board to Board Connector
Thu May 17, 2012 4:45 am by efung

» CPU Module Schematics
Thu May 17, 2012 2:37 am by odroid

» A stupid question : How can I root the ODROID-A
Wed May 16, 2012 7:05 am by Bingley

» ODROID-PC Ubuntu 12.04 with HDMI
Tue May 15, 2012 12:33 pm by grikukan

» ODROID-A4 IO Board - Oscilloscope
Tue May 15, 2012 11:48 am by tuxfield

» OPEN RFID Tag
Tue May 15, 2012 7:51 am by Alvaro

» About the recovery tool for Odroid-pc
Tue May 15, 2012 6:37 am by odroid


    Android SQL Demo

    Share

    Alvaro
    Admin
    Admin

    Devices: ODROID, ODROID-T, ODROID-7, ODROID-A, ODROID-ADK, ODROID-BaB, ODROID-PC, ODROID-E7, ODROID-A4
    Posts: 929
    Join date: 2010-05-09
    Location: Madrid, Spain

    Android SQL Demo

    Post by Alvaro on Sat Sep 25, 2010 12:40 pm

    Hi,

    SQL Demo


    This example shows how to interact with Android SDK internal database SQLite to perform a simple action of inserting and querying data from a database table and database display content.

    Our simple data format is: [row id] [time recorded] [Hello Android Event]



    To make this possible requires;

    1. An Activity SQLDemo

    In this example, the sqldemo activity has four important methods related to our database;
    AddEvent - ability to add a string type event into the database note the use of the getWritableDatabase() method to the database handler instance,
    GetEvents - ability to query database and here note the getReadableDatabase() method useage together with the startManagingCursor(cursor) and therefore return a cursor (walker),
    ShowEvents - display of the data submitted by GetEvents cursor that was returned and loop through all the data,
    onDestroy - always release database instance back to system;

    SQLDemo.java
    Code:

    package org.example.sqldemo;

    import static android.provider.BaseColumns._ID;
    import android.app.Activity;
    import android.content.ContentValues;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.os.Bundle;
    import android.widget.TextView;

    public class SQLDemo extends Activity {
      EventDataSQLHelper eventsData;
      TextView output;

      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        output = (TextView) findViewById(R.id.output);

        eventsData = new EventDataSQLHelper(this);
        addEvent("Hello Android Event");
        Cursor cursor = getEvents();
        showEvents(cursor);
      }
     
      @Override
      public void onDestroy() {
        eventsData.close();
      }

      private void addEvent(String title) {
        SQLiteDatabase db = eventsData.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(EventDataSQLHelper.TIME, System.currentTimeMillis());
        values.put(EventDataSQLHelper.TITLE, title);
        db.insert(EventDataSQLHelper.TABLE, null, values);

      }

      private Cursor getEvents() {
        SQLiteDatabase db = eventsData.getReadableDatabase();
        Cursor cursor = db.query(EventDataSQLHelper.TABLE, null, null, null, null,
            null, null);
       
        startManagingCursor(cursor);
        return cursor;
      }

      private void showEvents(Cursor cursor) {
        StringBuilder ret = new StringBuilder("Saved Events:\n\n");
        while (cursor.moveToNext()) {
          long id = cursor.getLong(0);
          long time = cursor.getLong(1);
          String title = cursor.getString(2);
          ret.append(id + ": " + time + ": " + title + "\n");
        }
        output.setText(ret);
      }
    }



    2. Database helper

    Handles all the database connections, creation of the necessary table and has additions like database upgrade information thrown in to code in case of future database upgrades.


    EventDataSQLHelper.java
    Code:

    package org.example.sqldemo;

    import android.content.Context;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;
    import android.provider.BaseColumns;
    import android.util.Log;

    /** Helper to the database, manages versions and creation */
    public class EventDataSQLHelper extends SQLiteOpenHelper {
        private static final String DATABASE_NAME = "events.db";
        private static final int DATABASE_VERSION = 1;

        // Table name
        public static final String TABLE = "events";

        // Columns
        public static final String TIME = "time";
        public static final String TITLE = "title";

        public EventDataSQLHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            String sql = "create table " + TABLE + "( " + BaseColumns._ID
                    + " integer primary key autoincrement, " + TIME + " integer, "
                    + TITLE + " text not null);";
            Log.d("EventsData", "onCreate: " + sql);
            db.execSQL(sql);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            if (oldVersion >= newVersion)
                return;

            String sql = null;
            if (oldVersion == 1)
                sql = "alter table " + TABLE + " add note text;";
            if (oldVersion == 2)
                sql = "";

            Log.d("EventsData", "onUpgrade    : " + sql);
            if (sql != null)
                db.execSQL(sql);
        }

    }


    Output


    Thanks,

    Alvaro


    _________________
    Alvaro

      Current date/time is Tue May 22, 2012 6:10 pm