Thursday, April 7, 2016

Registration Form with SQLite DataBase Part - I @ Android

"Jai Saraswati Maa"

Hi ... Dear All ..Today I am uploading a superb example of Uploading data into DataBase using SQLite.

Please check.
I am using Android Studio 1.5.1
Minimun SDK API 19
Target SDK API 23
Please Like us & put your valuable suggestions in comment box.
Special Thanks Yatendra Balodia (Student Partner)
Please add permissions in manifest file 

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">
     </uses-permission>

A. Layout 




B. XML file 


<?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="First Name"        android:id="@+id/tv1"

        android:layout_alignParentTop="true"

        android:layout_alignParentLeft="true"

        android:layout_alignParentStart="true"

        android:layout_marginTop="30dp" />
    <TextView        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

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

        android:text="Last Name"

        android:id="@+id/textView"

        android:layout_below="@+id/tv1"

        android:layout_alignRight="@+id/tv1"

        android:layout_alignEnd="@+id/tv1"

        android:layout_marginTop="74dp" />

    <EditText        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:inputType="textPersonName"

        android:ems="10"        android:id="@+id/et1"

        android:layout_alignTop="@+id/tv1"

        android:layout_alignParentRight="true"

        android:layout_alignParentEnd="true" />

    <EditText        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:inputType="textPersonName"

        android:ems="10"        android:id="@+id/et2"

        android:layout_alignBottom="@+id/textView"

        android:layout_alignRight="@+id/et1"

        android:layout_alignEnd="@+id/et1" />

    <Button        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="SAVE"        android:onClick="save"

        android:id="@+id/bt3"        android:layout_marginTop="54dp"

        android:layout_centerHorizontal="true"

        android:layout_centerVertical="true" />

</RelativeLayout>


C. Java Files
a.  Register.java  file   
 package yb7.mynewcrud;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class Register extends AppCompatActivity {
    EditText ed1,ed2;
    String str1,str2;
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.regact);
        ed1= (EditText) findViewById(R.id.et1);
        ed2= (EditText) findViewById(R.id.et2);
   }
    public void save(View v){
        str1=ed1.getText().toString();
        str2=ed2.getText().toString();
        MyHelper myhlp=new MyHelper(this);
        myhlp.putInfo(myhlp,str1,str2);
        Toast.makeText(this,"Record Saved",Toast.LENGTH_SHORT).show();
    }
}

b. MyHelper.java


package yb7.mynewcrud;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


public class MyHelper extends SQLiteOpenHelper {
    private static final String CREATE_QUERY= " create table " +
                         UserInfo.TAB_NAME + " ( " + UserInfo.KEY_ID 
                        " integer primary key autoincrement , " + UserInfo.F_NAME 
                        " varchar(20), "+ UserInfo.L_NAME + " varchar(20)) ;" ;
    public MyHelper(Context c){
        super(c,UserInfo.DB_NAME,null,UserInfo.DB_VER);
    }

    @Override    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_QUERY);
    }

    @Override

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL(" drop table if exists " + UserInfo.TAB_NAME);
        onCreate(db);
    }
    public void putInfo(MyHelper mob,String fname,String lname){
        SQLiteDatabase sq=mob.getWritableDatabase();
        ContentValues cv=new ContentValues();
        cv.put(UserInfo.F_NAME,fname);
        cv.put(UserInfo.L_NAME,lname);
        sq.insert(UserInfo.TAB_NAME,null,cv);
    }
}

c.  UserInfo.java file 
package yb7.mynewcrud;

public class UserInfo {
    public static final int DB_VER=1;
    public static final String KEY_ID="_id";
    public static final String F_NAME="fname";
    public static final String L_NAME="lname";
    public static final String DB_NAME="mydb2";
    public static final String TAB_NAME="usertab";
}

No comments:

Post a Comment