SQL Lite Database Save



Using Sqlite in Android application
Step 1.    Create a new Project.
Step 2.    Go to the design view and select the Text Fields section in the Palette and locate Plain Text
Step 3.    Drag and drop from the Form Widgets section into the layout and design a GUI as given below.

Untitled.jpg

Step 4.   Switch to the Text view, XML layout file and verify that the file looks similar to the following listing.

<?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:layout_width="match_parent"
         
android:layout_height="match_parent"
         
android:paddingBottom="@dimen/activity_vertical_margin"
         
android:paddingLeft="@dimen/activity_horizontal_margin"
         
android:paddingRight="@dimen/activity_horizontal_margin"
         
android:paddingTop="@dimen/activity_vertical_margin"
         
tools:context="lk.ac.ou.myapplication.MainActivity"
         
android:gravity = "center">

          <TextView
         
android:layout_width="wrap_content"
         
android:layout_height="wrap_content"
         
android:textAppearance="?android:attr/textAppearanceLarge"
         
android:text="Name"
         
android:id="@+id/textView"
         
android:layout_alignParentTop="true"
         
android:layout_alignParentLeft="true"
         
android:layout_alignParentStart="true" />
          <TextView
         
android:layout_width="wrap_content"
         
android:layout_height="wrap_content"
         
android:textAppearance="?android:attr/textAppearanceLarge"
         
android:text="Designation"
         
android:id="@+id/textView2"
         
android:layout_below="@+id/editText_name"
         
android:layout_alignParentLeft="true"
         
android:layout_alignParentStart="true" />

          <TextView
         
android:layout_width="wrap_content"
         
android:layout_height="wrap_content"
         
android:textAppearance="?android:attr/textAppearanceLarge"
         
android:text="Address"
         
android:id="@+id/textView3"
         
android:layout_below="@+id/editText_desi"
         
android:layout_alignParentLeft="true"
         
android:layout_alignParentStart="true" />

          <EditText
         
android:layout_width="match_parent"
         
android:layout_height="wrap_content"
         
android:id="@+id/editText_name"
         
android:layout_alignTop="@+id/textView"
         
android:layout_toRightOf="@+id/textView"
         
android:layout_toEndOf="@+id/textView" />



          <EditText
         
android:layout_width="match_parent"
         
android:layout_height="wrap_content"
         
android:id="@+id/editText_desi"
         
android:layout_alignTop="@+id/textView2"
         
android:layout_toRightOf="@+id/textView2"
         
android:layout_toEndOf="@+id/textView2" />

          <EditText
         
android:layout_width="match_parent"
         
android:layout_height="wrap_content"
         
android:id="@+id/editText_addr"
         
android:layout_below="@+id/editText_desi"
         
android:layout_toRightOf="@+id/textView3"
         
android:layout_toEndOf="@+id/textView3" />

          <Button
         
android:layout_width="wrap_content"
         
android:layout_height="wrap_content"
         
android:text="Add EMP"
         
android:id="@+id/button_add"
         
android:layout_below="@+id/editText_addr"
         
android:layout_alignParentLeft="true"
         
android:layout_alignParentStart="true"
         
android:layout_marginTop="76dp" />

          <Button
         
android:layout_width="wrap_content"
         
android:layout_height="wrap_content"
         
android:text="View All"
         
android:id="@+id/button_viewAll"
         
android:layout_above="@+id/button_update"
         
android:layout_centerHorizontal="true" />

          <Button
         
android:layout_width="wrap_content"
         
android:layout_height="wrap_content"
         
android:text="Update"
         
android:id="@+id/button_update"
         
android:layout_below="@+id/button_add"
         
android:layout_alignParentLeft="true"
         
android:layout_alignParentStart="true" />

          <Button
         
android:layout_width="wrap_content"
         
android:layout_height="wrap_content"
         
android:text="Delete"
         
android:id="@+id/button_delete"
         
android:layout_centerVertical="true"
          android:layout_below="@+id/button_viewAll"
          android:layout_alignLeft="@+id/button_viewAll"
         
android:layout_alignStart="@+id/button_viewAll" />

          <TextView
         
android:layout_width="wrap_content"
         
android:layout_height="wrap_content"
          android:textAppearance="?android:attr/textAppearanceLarge"
         
android:text="EMPID"
         
android:id="@+id/textView_id"
         
android:layout_below="@+id/editText_addr"
         
android:layout_alignParentLeft="true"
         
android:layout_alignParentStart="true" />

          <EditText
         
android:layout_width="match_parent"
         
android:layout_height="wrap_content"
         
android:id="@+id/editText_empid"
         
android:layout_alignTop="@+id/textView_id"
         
android:layout_toRightOf="@+id/textView3"
         
android:layout_toEndOf="@+id/textView3" />
</RelativeLayout>

Step 5.   Change your Activity class by adding the information on the view.

public class MainActivity extends AppCompatActivity

{
          DatabaseHelper myDb;
          EditText editName,editDesig,editAddre ,editTextEmpId;
          Button btnAddEMP;
          Button btnviewAll;
          Button btnDelete;
          Button btnviewUpdate;

          @Override
         
protected void onCreate(Bundle savedInstanceState)
          {
        super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          myDb = new DatabaseHelper(this);
          editName = (EditText)findViewById(R.id.editText_name);
          editDesig = (EditText)findViewById(R.id.editText_desi);
          editAddre = (EditText)findViewById(R.id.editText_addr);
          editTextEmpId = (EditText)findViewById(R.id.editText_empid);
          btnAddEMP = (Button)findViewById(R.id.button_add);
          btnviewAll = (Button)findViewById(R.id.button_viewAll);
          btnviewUpdate= (Button)findViewById(R.id.button_update);
          btnDelete= (Button)findViewById(R.id.button_delete);
          AddData();
          viewAll();
          UpdateData();
          DeleteData();
          }

          public void DeleteData() {
              btnDelete.setOnClickListener(
                   new View.OnClickListener() {
                   @Override
                  
public void onClick(View v) {
                             Integer deletedRows = myDb.deleteData(editTextEmpId.getText().toString());
                             if(deletedRows > 0)
                      Toast.makeText(MainActivity.this,"Data Deleted",Toast.LENGTH_LONG).show();
                             Else
  Toast.makeText(MainActivity.this,"Data not Deleted",Toast.LENGTH_LONG).show();
                   }
                   }
          );
          }

          public void UpdateData() {

          btnviewUpdate.setOnClickListener(
                   new View.OnClickListener() {
                   @Override
                  
public void onClick(View v) {
                             boolean isUpdate = myDb.updateData(editTextEmpId.getText().toString(),
                            editName.getText().toString(),
                            editDesig.getText().toString(),editAddre.getText().toString());
                             if(isUpdate == true)
                             Toast.makeText(MainActivity.this,"Data Update",Toast.LENGTH_LONG).show();
                             else
                            
Toast.makeText(MainActivity.this,"Data not Updated",Toast.LENGTH_LONG).show();
                   }
                   }

          );

          }

          public  void AddData() {
          btnAddEMP.setOnClickListener(
                   new View.OnClickListener() {
                      @Override
                  
public void onClick(View v) {
                             boolean isInserted = myDb.insertData(editName.getText().toString(),
                            editDesig.getText().toString(),
                                 editAddre.getText().toString() );
                             if(isInserted == true)
                      Toast.makeText(MainActivity.this,"Data Inserted",Toast.LENGTH_LONG).show();
                             else

                            
Toast.makeText(MainActivity.this,"Data not Inserted",Toast.LENGTH_LONG).show();

                   }

                   }

          );

          }

          public void viewAll() {
          btnviewAll.setOnClickListener(
                   new View.OnClickListener() {
                   @Override
                  
public void onClick(View v) {
                             Cursor res = myDb.getAllData();
                             if(res.getCount() == 0) {
                             // show message
                               
showMessage("Error","Nothing found");
                             return;
                             }

                             StringBuffer buffer = new StringBuffer();

                             while (res.moveToNext()) {
                             buffer.append("Id :"+ res.getString(0)+"\n");
                             buffer.append("Name :"+ res.getString(1)+"\n");
                             buffer.append("Surname :"+ res.getString(2)+"\n");
                             buffer.append("Marks :"+ res.getString(3)+"\n\n");
                             }
                             // Show all data
                            
showMessage("Data",buffer.toString());
                   }
                   }

          );

          }

          public void showMessage(String title,String Message){
          AlertDialog.Builder builder = new AlertDialog.Builder(this);
          builder.setCancelable(true);
          builder.setTitle(title);
          builder.setMessage(Message);
          builder.show();
          }

          @Override
         
public boolean onOptionsItemSelected(MenuItem item) {
          return super.onOptionsItemSelected(item);
          }
}

Step 6.   Run your application on an Android device

Comments

Popular posts from this blog

Google Map

SQL Lite Database Save