title: How to use Android SQLite Database
body: <p>When I am running the following example, The 'Add' button and 'Delete' button are not showing any error on tapping, but, when I tap the 'Show' button I am getting the error - "Unfortunately, learn has stopped."</p>
  
  <p>here is my mainactivity.java code -</p>
  
  <pre><code>package com.example.learn;
  
  
  import com.example.timepass.R;
  
  import android.os.Bundle;
  import android.app.Activity;
  import android.database.Cursor;
  import android.database.sqlite.SQLiteDatabase;
  import android.view.Menu;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  import android.widget.EditText;
  
  public class MainActivity extends Activity {
  
      Button b1, b2,b3;
      EditText t1,t2;
  
      SQLiteDatabase db;
      Cursor c;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          b1 = (Button) findViewById(R.id.button1);
          b2 = (Button) findViewById(R.id.button2);
          b3 = (Button) findViewById(R.id.button3);
          t1 = (EditText) findViewById(R.id.editText1);
          t2 = (EditText) findViewById(R.id.editText2);
  
  
          db = openOrCreateDatabase("MyDB", MODE_PRIVATE, null);
          db.execSQL("CREATE TABLE IF NOT EXISTS Names(Name VARCHAR)");
  
          b1.setOnClickListener(new OnClickListener() {
  
              @Override
              public void onClick(View arg0) {
                  // TODO Auto-generated method stub
  
                  if (t1.getText().toString().equals("")) {
                      t1.setText("Enter Name");
                  }
  
                  else {
                      String input = t1.getText().toString();
                      db.execSQL("INSERT INTO Names VALUES(" + "'" + input + "'"
                              + ")");
                  }
  
              }
          });
  
  
          b2.setOnClickListener(new OnClickListener() {
  
  
              @Override
              public void onClick(View arg0) {
                  // TODO Auto-generated method stub
                  c = db.rawQuery("SELECT * FROM Names",null);
                  int count = c.getCount();
  
                  t2.setText(count);
  
  
  
                }
          });
  
          b3.setOnClickListener(new OnClickListener() {
  
              @Override
              public void onClick(View arg0) {
                  // TODO Auto-generated method stub
                  db.execSQL("DELETE FROM Names");
              }
          });
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.activity_main, menu);
          return true;
      }
  
  }
  </code></pre>
  
  <p>and here is activity_main.xml code -</p>
  
  <pre><code>&lt;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"
      tools:context=".MainActivity" &gt;
  
      &lt;EditText
          android:id="@+id/editText1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentLeft="true"
          android:layout_alignParentRight="true"
          android:layout_alignParentTop="true"
          android:ems="10"
          android:hint="Add" &gt;
  
      &lt;/EditText&gt;
  
      &lt;Button
          android:id="@+id/button1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_below="@+id/editText1"
          android:layout_centerHorizontal="true"
          android:layout_marginTop="19dp"
          android:text="@string/add" /&gt;
  
      &lt;EditText
          android:id="@+id/editText2"
          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="36dp"
          android:ems="10"
          android:hint="Result" /&gt;
  
      &lt;Button
          android:id="@+id/button2"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignLeft="@+id/button1"
          android:layout_centerVertical="true"
          android:text="@string/show" /&gt;
  
      &lt;Button
          android:id="@+id/button3"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignLeft="@+id/button2"
          android:layout_alignParentBottom="true"
          android:layout_marginBottom="38dp"
          android:text="@string/del" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>I am learning android, so dont know much about it, Please help. Why am I getting the error?</p>