Managing SQLite Database in Android Studio : <p>I'm new to android application development. I'm trying to create a basic SQL Table with some values inserted.When I'm trying to extract the values from the table, some Exception is caught. </p>

<p>Can anyone please help.</p>

<pre><code>Database Helper:

package com.example.mathialagan.app1;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import static java.security.AccessController.getContext;

/**
 * Created by MATHIALAGAN on 3/12/2017.
 */

public class DatabaseHelper extends SQLiteOpenHelper {

public static final String db_name= "Employee_Details.db";
public static final String table_name="Table_1";
public static final String column_1= "ID";
public static final String column_2= "Name";
public static final String column_3= "Surname";
public static final String column_4= "Joining_Year";

public DatabaseHelper(Context context) {
    super(context, db_name, null, 1);
    //SQLiteDatabase db= this.getWritableDatabase()
}


@Override
public void onCreate(SQLiteDatabase db) {

   // db.execSQL("CREATE TABLE" + table_name + "(ID INTEGER PRIMARY KEY AUTOINCREMENT, Name TEXT, Surname TEXT, Joining_Year INTEGER)");


    String CREATE_DATABASE = "CREATE TABLE " + table_name + "("  +
           column_1 + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
         column_2 + "  TEXT, "+
            column_3 +" TEXT, "+
           column_4+ " TEXT"+")" ;

    db.execSQL(CREATE_DATABASE);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    db.execSQL("DROP TABLE IF EXISTS" + table_name);
    onCreate(db);

}


public boolean insertData (String name, String surname, String yr)
{

    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues cv = new ContentValues();

    cv.put(column_2, name);
   cv.put(column_3, surname);
    cv.put(column_4, yr);

    long result=db.insert(table_name, null, cv);

    if (result == -1)
        return false;
    else
        return true;

}

   public Cursor getAllData()
{
    SQLiteDatabase db = this.getWritableDatabase();

  //  Cursor res = db.rawQuery("SELECT * FROM" + table_name, null);

    Cursor res = db.rawQuery("SELECT * FROM" + table_name, null);
    return res;
}
</code></pre>

<p>}</p>

<p>MainActivity:</p>

<pre><code>package com.example.mathialagan.app1;

import android.database.Cursor;
import android.support.v7.app.AlertDialog;
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 MainActivity extends AppCompatActivity {

DatabaseHelper myDb;


EditText editName, editSurname, editYear , please;
Button btnAddData, btnView;

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

    myDb = new DatabaseHelper(this);

    editName = (EditText)findViewById(R.id.editText);
    editSurname = (EditText)findViewById(R.id.editText2);
    editYear = (EditText)findViewById(R.id.editText3);
    please = (EditText)findViewById(R.id.editText4) ;

    btnAddData = (Button)findViewById(R.id.button);
    btnView = (Button)findViewById(R.id.button2);

    adddata();

   viewData();

}

public void adddata()
{
    btnAddData.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    boolean isInserted = myDb.insertData(editName.getText().toString(),
                                                            editSurname.getText().toString(),
                                                                editYear.getText().toString());
                    if (isInserted == true)
                        Toast.makeText(MainActivity.this, "SUCCESS", Toast.LENGTH_SHORT).show();
                    else
                        Toast.makeText(MainActivity.this, "HAVE TO WORK MORE", Toast.LENGTH_SHORT).show();

                }
            }
    );
}

 public void viewData()
 {
       btnView.setOnClickListener(
              new View.OnClickListener() {
                  @Override
               public void onClick(View v) {


                   try {




                       Cursor res = myDb.getAllData();


                       if (res.getCount() == 0) {
                           Toast.makeText(MainActivity.this, "Nothing in DB", Toast.LENGTH_SHORT).show();
                           return;
                       } else {

                           StringBuffer bf = new StringBuffer();

                           while (res.moveToNext()) {
                               bf.append("ID:" + res.getString(0) + "\n");
                               bf.append("NAME:" + res.getString(1) + "\n");
                               bf.append("SURNAME:" + res.getString(2) + "\n");
                               bf.append("JOINING YEAR:" + res.getString(3) + "\n");

                           }

                           showMessage("Data " , bf.toString());


                       }
                   }

                   catch (Exception e)
                   {
                       Toast.makeText(MainActivity.this,"DB ", Toast.LENGTH_SHORT).show();
                   }



               }
           }
   );

  }


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();
}
</code></pre>

<p>}</p>
