Friday, February 24, 2012

Android SQLite Database Connectivity Simple Example

SQLite is an inbuilt database in android sdk tools....

you can access your database using SQLManager(https://addons.mozilla.org/en-US/firefox/addon/sqlite-manager/) or SQLBrowser(http://sourceforge.net/projects/asqlitemanager/)...

for it you have to use add-ons of morzilla firefox named SQLite Manager or download SQLite browser...
In eclipse at the top of the right side choose DDMS view and then go to the file explorer and choose your package name.....

then go to data/data/database.db......

then you have to export it using Pull a file from the device(icon is given at the right top side of the window)...
save the file usng .apk extention....

then open SQLManager(https://addons.mozilla.org/en-US/firefox/addon/sqlite-manager/) or SQLiteBrowser(http://sourceforge.net/projects/asqlitemanager/) and access it.....


Here is an android SQLite  Database Example...it is quiet simple to understand....
First create another xml file named as layout_main.xml in res\layout\...
the xml file will define the outer lookout of application...

Save it as layout_main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
    <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/tvw"
    android:text="Registration Form"
    />
    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="First Name"
    />
    <EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/edt1"
    />
    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Middle Name"
    />
    <EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/edt2"
    />
    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Last Name"
    />
    <EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/edt3"
    />
 
    <Button
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:text="Submit"
    android:id="@+id/btn">
    </Button>
 
    <Button
      android:layout_height="wrap_content"
      android:layout_width="fill_parent"
      android:text="Detail"
      android:id="@+id/btn3">
       </Button>
 
    <Button
      android:layout_height="wrap_content"
      android:layout_width="fill_parent"
      android:text="Delete All"
      android:id="@+id/btn1">
       </Button>
     
    <Button
      android:layout_height="wrap_content"
      android:layout_width="fill_parent"
      android:text="Update"
      android:id="@+id/btn2">
       </Button>
     
     <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/tvw2"
    />
 
 
</LinearLayout>



Save it as DataentryosingsqlActivity,java....


package com.dataentry;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

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

SQLiteDatabase db1 = null;
private static String DBNAME = "PERSONS.db";
Button btn,btn1,btn2,btn3 = null;
TextView tvw;
EditText edt1,edt2,edt3 = null;
Editable d1,d2,d3 = null;
 
@Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_main);
           edt1 = (EditText) findViewById(R.id.edt1);
           edt2 = (EditText) findViewById(R.id.edt2);
           edt3= (EditText) findViewById(R.id.edt3);
           btn = (Button) findViewById(R.id.btn);
         //Database will be created through below method
           db1 = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE,null);
//Create the table if it is not existing
       
           btn.setOnClickListener(new OnClickListener() {
        //handling the click method on button
@Override
public void onClick(View view) {
//For fetching data from edittext, use editable instead of string
d1 = edt1.getText();
d2 = edt2.getText();
d3 = edt3.getText();
try{

db1.execSQL("CREATE TABLE IF NOT EXISTS tabq34(ID INTEGER PRIMARY KEY ,FIRSTNAME VARCHAR, MIDDLENAME VARCHAR ,LASTNAME VARCHAR ); ");
   db1.execSQL("INSERT INTO tabq34 (FIRSTNAME,MIDDLENAME,LASTNAME)  VALUES ('"+d1+"','"+d2+"','"+d3+"');");
   Cursor c = db1.rawQuery("SELECT * FROM tabq34", null);
if(c!= null){
if (c.moveToFirst()) {
        do {
        //whole data of column is fetched by getColumnIndex()
        String firstname =c.getString(c.getColumnIndex("FIRSTNAME"));
        String middlename =c.getString(c.getColumnIndex("MIDDLENAME"));
        String lastname =c.getString(c.getColumnIndex("LASTNAME"));
        System.out.println(firstname);
        System.out.println(middlename);
        System.out.println(lastname);
          }while(c.moveToNext());}
//count the total number of entries
Integer a =  c.getCount();
System.out.println(a);
//db1.close();
//if you close the database then illegal exception will be occured...
}} catch(Exception e){
System.out.println(e);
}


           btn1 = (Button)  findViewById(R.id.btn1);
           btn1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View view) {
 db1.execSQL("DELETE  FROM tabq34 WHERE ID > -1;");
}
});


btn2 = (Button)  findViewById(R.id.btn2);
          btn2.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View view) {
 db1.execSQL("UPDATE tabq34 SET LASTNAME='SAM' WHERE FIRSTNAME='SAMKIT'");
}
});
       
          btn3 = (Button)  findViewById(R.id.btn3);
          btn3.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View view) {
tvw = (TextView) findViewById(R.id.tvw2);

tvw.setText(d1+""+d2+""+d3);
           String s = (String) tvw.getText();
System.out.print(s);
}
});
   }});
           }
}