Friday, 7 December 2012

Android Simple example using SQLLite DB

package com.example.databaseexample;

import android.R.string;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

    MyDbHelper mHelper;
    SQLiteDatabase mDb;;
    private String nameget, rankget, hobbyget;

    public String getNameget() {
        return nameget;
    }

    public void setNameget(String name2) {
        this.nameget = name2;
    }

    public String getRankget() {
        return rankget;
    }

    public void setRankget(String rankget) {
        this.rankget = rankget;
    }

    public String getHobbyget() {
        return hobbyget;
    }

    public void setHobbyget(String hobbyget) {
        this.hobbyget = hobbyget;
    }

    TextView sno, name, rank;
    EditText snoed, nameed, ranked;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mHelper = new MyDbHelper(this);
        mDb = mHelper.getWritableDatabase();
        sno = (TextView) findViewById(R.id.sno);
        name = (TextView) findViewById(R.id.name);
        rank = (TextView) findViewById(R.id.rank);
        snoed = (EditText) findViewById(R.id.snoedt0);
        nameed = (EditText) findViewById(R.id.nameedt1);
        ranked = (EditText) findViewById(R.id.rankedt2);

        sno.setText("HOBBY");
        name.setText("NAME");
        rank.setText("RANK");

        ContentValues cv = new ContentValues();

        cv.put(MyDbHelper.STUDENT_NAME, "namratha");
        cv.put(MyDbHelper.STUDENT_RANK, "one");
        cv.put(MyDbHelper.STUDENT_HOBBY, "singing");

        mDb.insert(MyDbHelper.TABLE_NAME, null, cv);
        System.out.println("insert success");

        Cursor cur = mDb.rawQuery("SELECT * from " + MyDbHelper.TABLE_NAME,
                null);

        if (cur != null) {
            if (cur.moveToFirst()) {

                String name = cur.getString(cur
                        .getColumnIndex(MyDbHelper.STUDENT_NAME));
                setNameget(name);
                String rank = cur.getString(cur
                        .getColumnIndex(MyDbHelper.STUDENT_RANK));
                setRankget(rank);
                String hobby = cur.getString(cur
                        .getColumnIndex(MyDbHelper.STUDENT_HOBBY));
                setHobbyget(hobby);

            }
        }

        nameed.setText(getNameget());
        ranked.setText(getRankget());
        snoed.setText(cur.getString(cur
                .getColumnIndex(MyDbHelper.STUDENT_HOBBY)));
    }

}



package com.example.databaseexample;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

class MyDbHelper extends SQLiteOpenHelper {
   
     private static final String DB_NAME = "mydb";
     private static final int DB_VERSION = 1;
//     private static final int SNO = 0;
   
     public static final String TABLE_NAME = "student";
     public static final String STUDENT_NAME = "sname";
     public static final String STUDENT_RANK = "srank";
     public static final String STUDENT_HOBBY="shobby";
   
   
   
     private static final String STRING_CREATE = "CREATE TABLE " + TABLE_NAME + " (_id INTEGER PRIMARY KEY AUTOINCREMENT, " + STUDENT_NAME + " TEXT, " + STUDENT_RANK + " TEXT ," + STUDENT_HOBBY + " TEXT "  +  ");";
   
     public MyDbHelper(Context context) {
     
     
     
      super(context, DB_NAME, null, DB_VERSION);
      System.out.println("db initialised");
     }
   
     @Override
     public void onCreate(SQLiteDatabase db) {
      db.execSQL(STRING_CREATE);
     
     
     }
   
     @Override
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
      db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
      onCreate(db);
     }
    }
    


Layout


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/sno"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="TextView" />

    <EditText
        android:id="@+id/snoedt0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/sno"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/snoedt0"
        android:layout_marginTop="43dp"
        android:text="TextView" />

    <EditText
        android:id="@+id/nameedt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:ems="10"
        android:inputType="textPersonName" />

    <TextView
        android:id="@+id/rank"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/nameedt1"
        android:layout_marginTop="51dp"
        android:text="TextView" />

    <EditText
        android:id="@+id/rankedt2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/rank"
        android:layout_marginTop="15dp"
        android:ems="10"
        android:inputType="textPersonName" />

</RelativeLayout>

No comments:

Post a Comment

Pass a HashMap from Angular Client to Spring boot API

This example is for the case where fileData is very huge and in json format   let map = new Map<string, string>()      map.set(this.ge...