Friday, March 31, 2017

CRUD Operations using SQLite - Mini App

Dear All

I am uploading a Mini App by which we can Register New user , View All registered user , Login , Update and Delete.
Database is done by using SQLite.
See  the app behavior through images
A.
a. Main Screen


b.  Registration Screen


c. Login Screen
 

d. View All

e. Update
     It will search first on the base of User Name

   
    If Search Successful it will show Username and Password for update
     

 


f. Delete
     For delete it will again search  - Delete Button disabled


     After User found Delete Button Enabled


 Hope you will manage xml files for Layout
B. Java Code is as
1. MainActivity.java
package com.example.bca_mobile_iv_18.sqlex1;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {
    Context con=MainActivity.this;
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void register(View v)
    {
        Intent registr=new Intent(con,RegistrationActivity.class);
        startActivity(registr);
    }
    public void viewuser(View v)
    {
        Intent usr=new Intent(con,Viewuser_activity.class);
        startActivity(usr);
    }
    public void login(View v)
    {
        startActivity(new Intent(con,Loginactivity.class));
    }
    public void delete(View v)
    {
        startActivity(new Intent(con,Deleteuseractivity.class));
    }
    public void update(View v)
    {
        startActivity(new Intent(con,Updateuseractivity.class));
    }
}

2. Helper class for Handling SQLite 

package com.example.bca_mobile_iv_18.sqlex1;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/** * Created by BCA-MOBILE-IV-18 on 3/28/2017. */
public class DbHelper extends SQLiteOpenHelper {
   public static final String key_id="_id";
   public static final String user_name="user_name";
   public static final String user_pass="user_pass";
   public static final String tab_name="myinfo";
   public static final String db_name="mynew.db";
    private static final int db_version=1;
    private static final String sql_query="create table "+ tab_name +" ( " + key_id +
                          " integer primary key autoincrement, " + user_name +
                          " text not null , " + user_pass + " text not null );";
        public DbHelper(Context con)
    {
        super(con,db_name,null,db_version);
    }

    @Override    public void onCreate(SQLiteDatabase db) {
        db.execSQL(sql_query);
        Log.d("rrrr","oncreate");

    }

    @Override    public void onUpgrade(SQLiteDatabase db, int oldVersion, 
                                        int newVersion) {
        db.execSQL("DROP TABLE IF EXIST " +tab_name);
        Log.d("hello","onupgrade");
        onCreate(db);
    }
    public void putinfo(DbHelper mob,String name,String pass)
    {
        SQLiteDatabase sq=mob.getWritableDatabase();
        ContentValues cv=new ContentValues();
        cv.put(user_name,name);
        cv.put(user_pass,pass);
        sq.insert(tab_name,null,cv);
    }
    public Cursor getALlRows(DbHelper mob)
    {
        SQLiteDatabase sq=mob.getReadableDatabase();
        Cursor cob=sq.query(tab_name,null,null,null,null,null,null);
        if(cob!=null)
        {
            cob.moveToFirst();
        }
        return cob;
    }
    public Cursor getuserinfo(DbHelper mob)
    {
        SQLiteDatabase sq=mob.getReadableDatabase();
        String columns[]={user_name,user_pass};
        Cursor cur=sq.query(tab_name,columns,null,null,null,null,null);
        return cur;
    }
    public Cursor getuserpass(DbHelper mob,String uname)
    {
        SQLiteDatabase sq=mob.getReadableDatabase();
        String columns[]={user_pass};
        String selection=user_name + " LIKE ? ";
        String args[]={uname};
        Cursor cur=sq.query(tab_name,columns,selection,args,null,null,null);
        return cur;
    }
    public void deleteuser(DbHelper mob,String uname,String upwd)
    {
        String selection=user_name + " LIKE ? AND " + user_pass + " LIKE ? ";
        String args[]={uname,upwd};
        SQLiteDatabase sq=mob.getWritableDatabase();
        sq.delete(tab_name,selection,args);
    }
    public void update(DbHelper mob,String uname,String upwd,
                                         String newuname,String newupwd)
    {
        String selection=user_name + " LIKE ? AND " + user_pass + " LIKE ? ";
        String args[]={uname,upwd};
        SQLiteDatabase sq=mob.getWritableDatabase();
        ContentValues cv=new ContentValues();
        cv.put(user_name,newuname);
        cv.put(user_pass,newupwd);
        sq.update(tab_name,cv,selection,args);
    }
}

3. DeleteUserActivity

package com.example.bca_mobile_iv_18.sqlex1;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Deleteuseractivity extends AppCompatActivity {
    EditText etuser,etpass;
    Button clear,validate,delete;
    String username,password;
    DbHelper myhelper;
    Context con=Deleteuseractivity.this;
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_deleteuseractivity);
        myhelper=new DbHelper(con);
        etuser=(EditText)findViewById(R.id.editText1);
        etpass=(EditText)findViewById(R.id.editText2);
        clear=(Button)findViewById(R.id.buttonclear);
        validate=(Button)findViewById(R.id.buttonvalidate);
        delete=(Button)findViewById(R.id.buttonupdate);
        delete.setEnabled(false);
        clear.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View v) {
                etuser.setText("");
                etpass.setText("");
            }
        });
        validate.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View v) {
                username=etuser.getText().toString();
                password=etpass.getText().toString();
                boolean checkflag=false;
                Cursor cur=myhelper.getuserinfo(myhelper);
                if(cur!=null) {
                    cur.moveToFirst();
                    do {
                        if (cur.getString(0).equalsIgnoreCase(username) &&
                                 cur.getString(1).equals(password)) {
                            checkflag = true;
                            username = cur.getString(0);
                            break;
                        }
                    } while (cur.moveToNext());
                    if (checkflag == true) {
                        Toast.makeText(con, "Validation success..!! \n Now press delete
                          user button to delete your account", Toast.LENGTH_SHORT).show();
                        delete.setEnabled(true);
                        checkflag = false;
                    } else {
                        Toast.makeText(con, "Validation failed.!! \n Invalid \
                             username/password", Toast.LENGTH_SHORT).show();
                        delete.setEnabled(false);
                    }
                }
                else                {
                    Toast.makeText(con, "Validation failed.!! \n Invalid username/
                     password \nNOTE: Database iS empty", Toast.LENGTH_SHORT).show();
                    delete.setEnabled(false);
                }
            }
        });
        delete.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View v) {
                myhelper.deleteuser(myhelper,username,password);
                Toast.makeText(con,username+" account successfully Deleted...!!",
                               Toast.LENGTH_SHORT).show();
                delete.setEnabled(false);
                startActivity(new Intent(con,MainActivity.class));
            }
        });
    }
}

4. Login Activity 

package com.example.bca_mobile_iv_18.sqlex1;

import android.content.Context;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Loginactivity extends AppCompatActivity {
    EditText etuser,etpass;
    Button clear,login;
    String username,password;
    Context con=Loginactivity.this;
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loginactivity);
        etuser=(EditText)findViewById(R.id.editText1);
        etpass=(EditText)findViewById(R.id.editText2);
        clear=(Button)findViewById(R.id.buttonclear);
        login=(Button)findViewById(R.id.buttonupdate);
        clear.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View v) {
                etuser.setText("");
                etpass.setText("");
            }
        });
        login.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View v) {
                username=etuser.getText().toString();
                password=etpass.getText().toString();
                boolean checkflag=false;
                DbHelper myhelper=new DbHelper(con);
                Cursor cur=myhelper.getuserinfo(myhelper);
                if(cur!=null) {
                    cur.moveToFirst();
                    do {
                        if (cur.getString(0).equalsIgnoreCase(username) &&
                               cur.getString(1).equals(password)) {
                            checkflag = true;
                            username = cur.getString(0);
                            break;
                        }
                    } while (cur.moveToNext());
                    if (checkflag == true) {
                        Toast.makeText(con, "Login success..!! \n Welcome: " + 
                              username, Toast.LENGTH_SHORT).show();
                        checkflag = false;
                    } else {
                        Toast.makeText(con, "Login failed.!! \n Invalid username/password",
                                   Toast.LENGTH_SHORT).show();
                    }
                }
                else                {
                    Toast.makeText(con, "Login failed.!! \n Invalid username/password
                            \nNOTE: Database iS empty", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}

5. Registration Activity 

package com.example.bca_mobile_iv_18.sqlex1;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class RegistrationActivity extends AppCompatActivity {
        EditText etname,etpass,etconformpass;
        String uname,upass,uconfirmpass;
        Context con=RegistrationActivity.this;
        Button buttonrgstr;
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registration);
        buttonrgstr=(Button)findViewById(R.id.buttonregister);
        etname=(EditText)findViewById(R.id.editText);
        etconformpass=(EditText)findViewById(R.id.editText3);
        etpass=(EditText)findViewById(R.id.editText2);
      buttonrgstr.setOnClickListener(new View.OnClickListener() {
          @Override          public void onClick(View v) {
              uname=etname.getText().toString();
              upass=etpass.getText().toString();
              uconfirmpass=etconformpass.getText().toString();
              if(upass.equals(uconfirmpass)) {
                  DbHelper myob = new DbHelper(con);
                  myob.putinfo(myob, uname, upass);
                  Toast.makeText(getBaseContext(), "Registered Successfully...!!", 
                           Toast.LENGTH_LONG).show();
                  finish();
                  startActivity(new Intent(con, MainActivity.class));
              }
              else              {  Toast.makeText(getBaseContext(), "Password does not match...!!", 
                  Toast.LENGTH_LONG).show();  }
          }
      });
    }
} 

6. Update Activity 

package com.example.bca_mobile_iv_18.sqlex1;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Updateuseractivity extends AppCompatActivity {
    EditText etuser,etpass;
    Button clear,search,update;
    String username,password,newusername,newpassword;
    DbHelper myhelper;
    Context con=Updateuseractivity.this;
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_updateuseractivity);
        myhelper=new DbHelper(con);
        etuser=(EditText)findViewById(R.id.editText1);
        etpass=(EditText)findViewById(R.id.editText2);
        clear=(Button)findViewById(R.id.buttonclear);
        search=(Button)findViewById(R.id.buttonsearch);
        update=(Button)findViewById(R.id.buttonupdate);
        update.setEnabled(false);
        etpass.setEnabled(false);

        clear.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View v) {
                etuser.setText("");
                etpass.setText("");
            }
        });
        search.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View v) {
                username=etuser.getText().toString();
                boolean checkflag=false;
                Cursor cur=myhelper.getuserpass(myhelper,username);
                if(cur!=null) {
                    cur.moveToFirst();
                    password = cur.getString(0);
                    etpass.setEnabled(true);
                    etpass.setText(password);
                    update.setEnabled(true);
                }
                else                {
                    Toast.makeText(con,"User: "+username+" Not Found...!!",
                      Toast.LENGTH_SHORT).show();
                    update.setEnabled(true);
                }
            }
        });
        update.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View v) {
                newusername=etuser.getText().toString();
                newpassword=etpass.getText().toString();
               myhelper.update(myhelper, username, password, newusername, newpassword);
              Toast.makeText(con, "Updation successful...!!\n updated details:-
                       \n username: " + newusername + "\n password: " + newpassword,
                       Toast.LENGTH_LONG).show();
                    update.setEnabled(false);
                    startActivity(new Intent(con, MainActivity.class));
            }
        });
    }
}

7. View All User 

package com.example.bca_mobile_iv_18.sqlex1;

import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.SimpleCursorAdapter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;

public class Viewuser_activity extends AppCompatActivity {
        Context con=Viewuser_activity.this;
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_viewuser_activity);
        showListView();
    }
    public void showListView()
    {
    DbHelper myhelper =new DbHelper(con);
        Cursor cursorOB=myhelper.getALlRows(myhelper);
        String[] fromDB=new String[]{DbHelper.key_id,DbHelper.user_name,
                      DbHelper.user_pass};
        int toShow[]=new int[]{R.id.textView1,R.id.textView2,R.id.textView3};
        SimpleCursorAdapter simpleCursorAdapter=new SimpleCursorAdapter
                  (getBaseContext(),R.layout.onerow,cursorOB,fromDB,toShow,0);
        ListView listView=(ListView)findViewById(R.id.listview);
        listView.setAdapter(simpleCursorAdapter);
    }
}


Layout for single row onerow.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"    android:weightSum="3">

    <LinearLayout        android:orientation="horizontal"

        android:layout_width="match_parent"

        android:layout_height="50dp"

        android:layout_weight="1"

        android:weightSum="3">

    <TextView        android:text="TextView"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:textAlignment="center"

        android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"

        android:id="@+id/textView1"

        android:layout_weight="0.70" />

    <TextView        android:text="TextView"

        android:layout_width="98dp"        android:layout_height="wrap_content"
\        android:textAlignment="center"

        android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"

        android:id="@+id/textView2"

        android:layout_weight="1"/>
    <TextView        android:text="TextView"

        android:layout_width="98dp"

        android:layout_height="wrap_content"

        android:textAlignment="center"

        android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"

        android:id="@+id/textView3"

        android:layout_weight="1"        />
    </LinearLayout>
</LinearLayout>

Sunday, March 12, 2017

SQLite Example - Register New User and View All Registered User

Dear All

" Happy Holi......"

What is SQLite :

SQLite is an in-process Library used to work with Database for Mobile OS(Mostly).
@ It's Server-Less
@ Already Configured
@ Follow ACID Property
@ Structured Database
@ No Dependency

How to work with Database in Android  :
Step 1 : Define Schema
             " Means Define Database name , table name , column name ..........."
Step 2: Create Database
             a.  Create a Class by extending SQLiteOpenHelper
             b.  Override  onCreate(SQLiteDatabase ob) method -
                   Like ob.execSQL(" String query")
             c. Override onUpgrade(SQLiteDatabase db, int oldVersion, int                                                 newVersion)
          if any alter in Table 
Step 3: Execute Query 
       a. Create object of SQLiteDatabase   either by                                  using getWritableDatabase() or 
          getReadableDatabase()  with the help of Helper class                         object 
       b. Use ContentValues  class object to put like key-value pair                   data
       c. Execute query like insert() , update().... by                               using SQLiteDatabase object

Just See the example :
A. Output will be like
     a. First Screen - Launched Activity

    XML Code for this
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context="com.example.ravigodara.sql2017ex1.MainActivity"

    tools:layout_editor_absoluteY="81dp"

    tools:layout_editor_absoluteX="0dp">

    <Button        android:id="@+id/button"

        android:layout_width="368dp"

        android:layout_height="48dp"

        android:layout_alignEnd="@+id/button2"

        android:layout_alignParentTop="true"

        android:layout_marginTop="62dp"

        android:text="Register"

        tools:layout_editor_absoluteX="8dp"

        tools:layout_editor_absoluteY="45dp"

        android:onClick="reg"/>

    <Button        android:id="@+id/button2"

        android:layout_width="368dp"        android:layout_height="48dp"

        android:text="Registered User"

        tools:layout_editor_absoluteX="8dp"   tools:layout_editor_absoluteY="151dp"

        android:layout_alignParentBottom="true"

        android:layout_centerHorizontal="true"

        android:layout_marginBottom="109dp"

        android:onClick="showData"/>
</RelativeLayout> 
 b.  Second Screen - When you click on Register


XML Code for this layout
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent" android:layout_height="match_parent">
    <TextView        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:textAppearance="?android:attr/textAppearanceLarge"

        android:text="UserName"        android:id="@+id/textView"

        android:layout_alignParentTop="true"

        android:layout_alignParentStart="true" />

    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:textAppearance="?android:attr/textAppearanceLarge"

        android:text="Password"        android:id="@+id/textView2"

        android:layout_below="@+id/textView"

        android:layout_alignEnd="@+id/textView"

        android:layout_marginTop="44dp" />

    <EditText        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:id="@+id/userN"     android:layout_alignParentTop="true"

        android:layout_alignParentEnd="true"

        android:layout_toEndOf="@+id/textView" />

    <EditText

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"      android:id="@+id/userP"

        android:inputType="textPassword"

        android:layout_alignBottom="@+id/textView2"

        android:layout_alignParentEnd="true"

        android:layout_toEndOf="@+id/textView2" />

    <Button        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Register"

        android:id="@+id/button"

        android:layout_centerVertical="true"

        android:layout_centerHorizontal="true" />
</RelativeLayout> 
  c. Third Screen - All registered User


XML Code for this layout
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context="com.example.ravigodara.sql2017ex1.ViewData">

    <ListView

        android:layout_width="wrap_content"   android:layout_height="wrap_content"

        android:id="@+id/listView"

        android:layout_alignParentStart="true"

        android:layout_alignParentTop="true" />
</RelativeLayout>

Single Row Layout File 

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="horizontal" android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:weightSum="1">

    <TextView

        android:layout_width="wrap_content"    android:layout_height="39dp"

        android:text="New Text"        android:gravity="center"

        android:id="@+id/text_id"        android:layout_weight="0.06" />

    <TextView        android:layout_width="wrap_content"

        android:layout_height="41dp"

        android:text="New Text"        android:gravity="center"

        android:id="@+id/text_name"        android:layout_weight="0.90" />
    <TextView        android:layout_width="159dp"

        android:layout_height="37dp"

        android:text="New Text"        android:gravity="center"

        android:id="@+id/text_pass" />
</LinearLayout>

B. Java Code

a. MainActivity.java   code is

package com.example.ravigodara.sql2017ex1;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void reg(View view)
    {
        startActivity(new Intent(this, Register.class));
    }
    public void showData(View view)
    {
        Intent iob = new Intent(this,ViewData.class);
        startActivity(iob);
    }
}

b. Register.java code is as

package com.example.ravigodara.sql2017ex1;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/** * Created by Ravi Godara on 3/10/2017. */
public class Register extends AppCompatActivity {

    EditText etus, etpass;
    String struser, strpass;
    Button reg;
    Context cob = this;

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.regist);
        etus = (EditText) findViewById(R.id.userN);
        etpass = (EditText) findViewById(R.id.userP);

        reg = (Button) findViewById(R.id.button);
        reg.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View v) {
                struser = etus.getText().toString();
                strpass = etpass.getText().toString();


                DbHelper myob = new DbHelper(cob);
                    myob.putInfo(myob, struser, strpass);
                    Toast.makeText(getBaseContext(), 
                          "Registered Successfully", Toast.LENGTH_LONG).show();
                    finish();

            }
        });
    }
}

c. To See all Registered User is as

package com.example.ravigodara.sql2017ex1;

import android.content.Context;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class ViewData extends AppCompatActivity {
    Context context = this;

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

    private void showListView() {
        DbHelper myHelper = new DbHelper(context);
        Cursor cursorOb = myHelper.getAllRows(myHelper);
        String[] fromDB = new String[]{DbHelper.KEY_ID, DbHelper.USER_NAME, 
                           DbHelper.USER_PAS};
        int[] toShow = new int[]{R.id.text_id, R.id.text_name, R.id.text_pass};
        SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter
                 (getBaseContext(), R.layout.onerow, cursorOb, fromDB, toShow, 0);
        ListView listView = (ListView) findViewById(R.id.listView);
        listView.setAdapter(simpleCursorAdapter);
    }
}

d. DataBase Helper class is as
package com.example.ravigodara.sql2017ex1;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DbHelper extends SQLiteOpenHelper {
    public static final String KEY_ID="_id";
    public static final String USER_NAME="user_name";
    public static final String USER_PAS="user_pass";
    private static final String DB_NAME="mydb.db";
    private static final String TAB_NAME="myinfo";
    private static final int DB_VER=1;
    private static final  String CREATE_QUERY = "create table " + TAB_NAME + " ( "
                    + KEY_ID + " integer primary key autoincrement, " +
            USER_NAME + " text not null, " + USER_PAS + " text not null );";

    public DbHelper(Context context)
    {
        super(context,DB_NAME,null,1);
    }
    @Override    public void onCreate(SQLiteDatabase sdb) {

        sdb.execSQL(CREATE_QUERY);
        Log.d("rrrr", "oncreate");
    }

    @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
     {
        db.execSQL("DROP TABLE IF EXISTS " + TAB_NAME);
        Log.d("hello", "onupgrade");
        onCreate(db);

    }
    public  void putInfo(DbHelper mob,String name,String pass)
    {
        SQLiteDatabase SQ= mob.getWritableDatabase();
        ContentValues CV=new ContentValues();
        CV.put(USER_NAME, name);
        CV.put(USER_PAS, pass);
        SQ.insert(TAB_NAME, null, CV);
    }

    public Cursor getAllRows(DbHelper mob)
    {
        SQLiteDatabase SQ = mob.getReadableDatabase();
        Cursor cob = SQ.query(TAB_NAME,null,null,null,null,null,null);
        if(cob!=null)
        {
            cob.moveToFirst();
        }
        return cob;
    }
}