Post Top Ad

Post Top Ad

Saturday 8 July 2017

Android SQLite Database

SQLite is a opensource SQL database that stores data to a text file on a device. Android comes in with built in SQLite database implementation.

 1 activity_main.xml::

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.kiran.lesson14_sqlitedatabase.MainActivity">


    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:hint="Name"
        android:ems="10"
        android:id="@+id/name_et" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:hint="Phone"
        android:ems="10"
        android:id="@+id/phone_et" />

    <Button
        android:text="Insert"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="insertData"
        android:id="@+id/insert_btn" />

    <Button
        android:text="Read"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="readData"
        android:id="@+id/button2" />


</LinearLayout>

2.MainActivity.java

package com.example.kiran.lesson14_sqlitedatabase;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private EditText mPhoneEt;
    private EditText mNameEt;
    private MyOpenHelper mMyOpenHelper;
    private SQLiteDatabase mDb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mNameEt = (EditText) findViewById(R.id.name_et);
        mPhoneEt = (EditText) findViewById(R.id.phone_et);
        mMyOpenHelper = new MyOpenHelper(MainActivity.this,"STUD",null,1);
        mDb = mMyOpenHelper.getWritableDatabase();

    }

    public void insertData(View view) {
        String mName = mNameEt.getText().toString();
        String mPhone = mPhoneEt.getText().toString();
        ContentValues cv = new ContentValues();
        cv.put("name",mName);
        cv.put("phone",mPhone);
        long id = mDb.insert("student",null,cv);
        Toast.makeText(MainActivity.this,"Saved"+id,Toast.LENGTH_SHORT).show();
    }

    public void readData(View view) {
        Cursor cursor = mDb.query("student",null,null,null,null,null,null);
        while (cursor.moveToNext()){
            Toast.makeText(MainActivity.this,"Name: "+cursor.getString(1)+" Phone: "+cursor.getString(2),Toast.LENGTH_SHORT).show();
        }
    }
}

3 MyOpenHelper.java:

package com.example.kiran.lesson14_sqlitedatabase;

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

/**
 * Created by kiran on 11/10/2016.
 */

public class MyOpenHelper extends SQLiteOpenHelper {
    public MyOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("Create table student(_id integer primary key, name text,phone text)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

 

No comments:

Post a Comment