Pages

Saturday, May 21, 2016

Simple SQLiteDatabase Example in Android

activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="34dp"
        android:layout_marginStart="34dp"
        android:layout_marginTop="118dp"
        android:text="First Name ="
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/first"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView2"
        android:layout_marginLeft="31dp"
        android:layout_marginStart="31dp"
        android:layout_toEndOf="@+id/textView2"
        android:layout_toRightOf="@+id/textView2"
        android:text="First"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_alignStart="@+id/textView2"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="35dp"
        android:text="Last Name ="
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/second"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/first"
        android:layout_alignStart="@+id/first"
        android:layout_alignTop="@+id/textView3"
        android:text="Second"
        android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
MainAcvtivity.java
package com.blogspot.kantapp.simplesqlitedatabaseexampleinandroid;

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

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SQLiteDatabase db = openOrCreateDatabase("kantdb", MODE_PRIVATE, null);

        db.execSQL("create table if not exists sampletable(firstname varchar,lastname varchar)");

        db.execSQL("insert into sampletable values('Arvind','kant')");

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

        c.moveToFirst();

//Getting data using column name
        String fname = c.getString(c.getColumnIndex("firstname"));

//Getting data using column index number
        String lname = c.getString(1);

        Toast.makeText(getApplicationContext(), "firstname :"+fname+"\n"+"lastname :"+lname, Toast.LENGTH_LONG).show();

        TextView editText=(TextView)findViewById(R.id.first);
        TextView editText1=(TextView)findViewById(R.id.second);

        editText.setText(fname);
        editText1.setText(lname);

    }
}

No comments:

Post a Comment