Sunday, 27 January 2013

Android with SQLite Database Connectivity in Simple Way

Hai Friends today i am going to share about how we can connect android with SQLite Database in Simple way for Efficient Purpose..
Try this program on Android Ginger bread Version.

Snapshots:










Copy and Paste following code into Eclipse and Run the Program.

LoginSecurity.java
package login.log;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class LoginSecurity {
 String DbName="NewLogin";
 String DbTable="logi";
 int DbVersion=1;
 String Key_Name="Name";
 String Key_Pass="Password";
 String CreateTable="create table logi(Name text not null, Password text not null);";
 sqlitehelper shelp;
 SQLiteDatabase sbase;
 Context con;
 public LoginSecurity(Context c)
 {
  con=c;
 }
 public LoginSecurity openforread() throws SQLException
 {
  shelp=new sqlitehelper(con, DbName, null, DbVersion);
  sbase=shelp.getReadableDatabase();
  return this;
 }
 public LoginSecurity openforwrite() throws SQLException
 {
  shelp=new sqlitehelper(con, DbName, null, DbVersion);
  sbase=shelp.getWritableDatabase();
  return this;
 }
 public void close()
 {
  shelp.close();
 }
 public long insert(String s1, String s2)
 {
  ContentValues cv=new ContentValues();
  cv.put(Key_Name, s1);
  cv.put(Key_Pass, s2);
  return sbase.insert(DbTable, null, cv);
 }
 public String getPass(String pa)
 {
  String[] col=new String[]{Key_Name, Key_Pass};
  Cursor c=sbase.query(DbTable, col, null, null, null, null, null);
  String result="";
  int index=c.getColumnIndex(Key_Pass);
  for(c.moveToFirst();!c.isAfterLast();c.moveToNext())
  {
   result=c.getString(index);
  }
  return result;
  
 }
 
 public class sqlitehelper extends SQLiteOpenHelper
 {

   public sqlitehelper(Context context, String name,
    CursorFactory factory, int version) {
   super(context, name, factory, version);
   // TODO Auto-generated constructor stub
  }

   @Override
  public void onCreate(SQLiteDatabase db) {
   // TODO Auto-generated method stub
   db.execSQL(CreateTable);
   
  }

   @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
   // TODO Auto-generated method stub
   
  }
  
 }

}



LoginSeminar.java
package login.log;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class LoginSeminar extends Activity {
    /** Called when the activity is first created. */
 Button btsub,btcan;
 Button btnreg;
 EditText myuser,mypass;
 LoginSecurity ls;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btnreg=(Button)findViewById(R.id.button3);
        btcan=(Button)findViewById(R.id.button2);
        myuser=(EditText)findViewById(R.id.editText1);
        mypass=(EditText)findViewById(R.id.editText2);
        btsub=(Button)findViewById(R.id.button1);
        ls=new LoginSecurity(this);
        try
        {
        
        btsub.setOnClickListener(new View.OnClickListener() {
   
   public void onClick(View v) {
    // TODO Auto-generated method stub
    String x=myuser.getText().toString();
    String y=mypass.getText().toString();
    String temp="";
    ls.openforread();
    temp=ls.getPass(x);
    ls.close();
    if(temp.equals(y))
    {
     Toast.makeText(getApplicationContext(), "You are valid user", Toast.LENGTH_SHORT).show();
     
    }
    else
    {
     Toast.makeText(getApplicationContext(), "Sorry u r not valid user", Toast.LENGTH_SHORT).show();
    }
    
   }
  });
        }
        catch(Exception e)
        {
         Toast.makeText(getApplicationContext(), "ur login not exists pls register here", Toast.LENGTH_SHORT).show();
        }
        btnreg.setOnClickListener(new View.OnClickListener() {
   
   public void onClick(View v) {
    // TODO Auto-generated method stub
    Intent i=new Intent(getApplicationContext(), RegisterAccount.class);
    startActivity(i);
    
   }
  });
    }
}



RegisterAccount.java
package login.log;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class RegisterAccount extends Activity {
 EditText nwuser,nwpass;
 Button btnreg;
 Button btnbk;
 LoginSecurity lss;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main1);
  nwuser=(EditText)findViewById(R.id.etnewuser);
  nwpass=(EditText)findViewById(R.id.etnewpass);
  btnreg=(Button)findViewById(R.id.btreg);
  btnbk=(Button)findViewById(R.id.btback);
  lss=new LoginSecurity(this);
  btnreg.setOnClickListener(new View.OnClickListener() {
   
   public void onClick(View v) {
    // TODO Auto-generated method stub
    try
    {
    String us=nwuser.getText().toString();
    String ps=nwpass.getText().toString();
    lss.openforwrite();
    lss.insert(us, ps);
    lss.close();
    Toast.makeText(getApplicationContext(), "Your Account Was Created", Toast.LENGTH_SHORT).show();
    }
    catch(Exception e)
    {
     e.printStackTrace();
    }
    
    
   }
  });
  btnbk.setOnClickListener(new View.OnClickListener() {
   
   public void onClick(View v) {
    // TODO Auto-generated method stub
    Intent ii=new Intent(getApplicationContext(), LoginSeminar.class);
    startActivity(ii);
    
   }
  });
  
 }
 
 
}



main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/RelativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/background"
    android:orientation="vertical"
    tools:context=".LoginSeminar" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="83dp"
        android:text="User Name"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="82dp"
        android:text="Password"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView1"
        android:layout_alignBottom="@+id/textView1"
        android:layout_alignParentRight="true"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView2"
        android:layout_alignBottom="@+id/textView2"
        android:layout_alignParentRight="true"
        android:ems="10" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText2"
        android:layout_marginTop="56dp"
        android:layout_toRightOf="@+id/textView2"
        android:text="Submit" />


    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button1"
        android:layout_alignBottom="@+id/button1"
        android:layout_marginLeft="55dp"
        android:layout_toRightOf="@+id/button1"
        android:text="Cancel" />



    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/button1"
        android:layout_marginTop="45dp"
        android:text="Register ur New Account" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_alignParentTop="true"
        android:text="Login Form"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textStyle="bold" />

</RelativeLayout>



main1.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/background"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:text="Password"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="89dp"
        android:text="User Name"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/etnewuser"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView1"
        android:layout_alignBottom="@+id/textView1"
        android:layout_marginLeft="27dp"
        android:layout_toRightOf="@+id/textView1"
        android:ems="10" >

        <requestFocus />
    </EditText>


    <EditText
        android:id="@+id/etnewpass"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView2"
        android:layout_alignBottom="@+id/textView2"
        android:layout_alignLeft="@+id/etnewuser"
        android:layout_alignParentRight="true"
        android:ems="10" />


    <Button
        android:id="@+id/btreg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/etnewpass"
        android:layout_marginTop="71dp"
        android:layout_toRightOf="@+id/textView1"
        android:text="Register" />




    <Button
        android:id="@+id/btback"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/btreg"
        android:layout_alignRight="@+id/btreg"
        android:layout_below="@+id/btreg"
        android:layout_marginTop="27dp"
        android:text="Back" />



    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/textView2"
        android:text="Registration Form"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textStyle="bold" />

</RelativeLayout>



AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="login.log"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".LoginSeminar"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".RegisterAccount"
            android:label="@string/app_name" >
            </activity>
    </application>

</manifest>




Thanks For Visiting....

1 comment:

  1. Android – Creating Registration Page using SQLITE Database

    http://apptronix.net/registration-page-sqlite/

    ReplyDelete