Thursday 7 March 2013

SQLiteDatabase



 
Activity:
 


package com.venky.sqlitedatabase;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.Toast;

public class SQLiteDatabaseDemoActivity extends Activity {
      /** Called when the activity is first created. */

      SQLiteDatabase db;

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

            //Create Database
            db = openOrCreateDatabase("employee", 1, null);

            //Create Table
            db.execSQL("create table if not exists sampletable(name varchar not null,
                                    location text not null)");

            //Insert Values
            db.execSQL("insert into sampletable values('venky','hyd')");

            //Get All Values
            Cursor c = db.rawQuery("select * from sampletable", null);

            //Move Cursor position into First
            c.moveToFirst();

            while (c.moveToNext()) {

                  //Getting Values into Cursor
                  String name = c.getString(0);
                  String location = c.getString(c.getColumnIndex("location"));

                  //Display Values
                  Toast.makeText(getApplicationContext(), name + "-" + location, 3000)
                              .show();

            }
      }
}



No comments:

Post a Comment