title: Android studio! Add, Edit and Delete records using SQLite
body: <p>I am an extreme beginner at android studio and using JavaScript, so still learning and trying to grasp new concepts of android studio as a whole.</p>
  
  <p>I am trying to create a database where it adds, edits and deletes records that a user manually inputs.</p>
  
  <p>When I try to add a new record from the friends.xml page it seems to not come up with any records on the view data page. <strong>It doesn't seem to record the data entry?</strong></p>
  
  <p><strong>friends.java</strong> (page does that most of the handling)</p>
  
  <pre><code>    package com.example.chris.mobileappsassignment;
  
  import android.content.Intent;
  import android.os.Bundle;
  import android.support.design.widget.FloatingActionButton;
  import android.support.design.widget.Snackbar;
  import android.support.v7.app.AppCompatActivity;
  import android.support.v7.widget.Toolbar;
  import android.view.View;
  import android.widget.AdapterView;
  import android.widget.ArrayAdapter;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.Spinner;
  import android.widget.Toast;
  
  public class friends extends AppCompatActivity {
  
  
      private static final String TAG = "friends";
  
      DatabaseHelper mDatabaseHelper;
      private Button addbutton, viewdatabutton;
      private EditText firstnameinput, lastnameinput, ageinput, addressinput;
  
  
  
  
  
      /////////////////////////////////////////////////////////////////////////////
      // SPINNER
      /////////////////////////////////////////////////////////////////////////////
  
      Spinner spinner;
      ArrayAdapter&lt;CharSequence&gt; adapter;
  
  
      /////////////////////////////////////////////////////////////////////////////
      //// END SPINNER
      ////////////////////////////////////////////////////////////////////////////
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_friends);
  
          // DECLARE EDIT TEXT'S
  
          firstnameinput = (EditText) findViewById(R.id.firstnameinput);
          lastnameinput = (EditText) findViewById(R.id.lastnameinput);
          ageinput = (EditText) findViewById(R.id.ageinput);
          addressinput = (EditText) findViewById(R.id.addressinput);
  
          //DECLARE BUTTONS
          addbutton = (Button) findViewById(R.id.addbutton);
          viewdatabutton = (Button) findViewById(R.id.viewdatabutton);
  
          //DECLARE SPINNER
          spinner = (Spinner) findViewById(R.id.male_female);
  
          mDatabaseHelper = new DatabaseHelper(this);
  
  
  
  
          // ADD BUTTON
  
  
  
          addbutton.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
  
                  boolean addOK = true;
                  int age_as_int = -1;
  
                  String firstName = firstnameinput.getText().toString();
                  String lastName = lastnameinput.getText().toString();
                  String age = ageinput.getText().toString();
                  String address = addressinput.getText().toString();
  
                  if (firstName.length() &lt; 1) {
                      toastMessage("You must enter something in this field!");
                      firstnameinput.requestFocus();
                      addOK = false;
                  }
                  if (lastName.length() &lt; 1) {
                      toastMessage("You must enter something in this field!");
                      lastnameinput.requestFocus();
                      addOK = false;
                  }
                  if (age.length() &lt; 1) {
                      toastMessage("You must enter something in this field!");
                      ageinput.requestFocus();
                      addOK = false;
                  }
                  if (address.length() != 0) {
                      toastMessage("You must enter something in this field!");
                      addressinput.requestFocus();
                      addOK = false;
                  }
                  try {
                      age_as_int = Integer.parseInt(age);
                  } catch (NumberFormatException e) {
                      toastMessage("You must enter a valid Number in this field!");
                      ageinput.requestFocus();
                      addOK = false;
                  }
  
                  if (addOK) {
                      AddData(firstName,lastName,"????",age_as_int,address);
                      toastMessage("Friend Added!");
                  }
  
              }
          }); // end addbutton onclicklistener
  
  
  
  
  
  
          // VIEW DATA BUTTON &gt;&gt;&gt;&gt; LINKS TO viewdata.xml (listview)
  
  
  
          viewdatabutton.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  Intent intent = new Intent(friends.this, listdata.class );
                  startActivity(intent);
              }
          });
  
  
  
  
  
  /*
          Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
          setSupportActionBar(toolbar);
  */
  
  
  
          /////////////////////////////////////////////////////////////////////////////
          //SPINNER
          /////////////////////////////////////////////////////////////////////////////
  
          spinner = (Spinner)findViewById(R.id.male_female); //id of the spinner
          adapter = ArrayAdapter.createFromResource(this, R.array.male_female,android.R.layout.simple_spinner_item);
          adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
          spinner.setAdapter(adapter);
          spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                                                @Override
                                                public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int position, long id) {
                                                    Toast.makeText(getBaseContext(),parent.getItemAtPosition(position)+" selected",Toast.LENGTH_LONG).show();
                                                }
  
                                                @Override
                                                public void onNothingSelected(AdapterView&lt;?&gt; parent) {
  
                                                }
                                            });
  
  
          /////////////////////////////////////////////////////////////////////////////
          //// END SPINNER
          ////////////////////////////////////////////////////////////////////////////
  
                  FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
          fab.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_SHORT)
                          .setAction("Action", null).show();
              }
          });
  
  
      }  // ON CREATE CLOSE
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
      *// ADD DATA
  
      public void AddData(String firstName, String lastName, String s, int age_as_int, String newEntry) {
          boolean insertData = mDatabaseHelper.addData(newEntry);
  
          if (insertData) {
              toastMessage("Data Successfully Added!");
          } else {
                  toastMessage("Something has gone wrong :(");
              }
          }*
  
  
  
  
  
  
  
  
  
  
  
  
  
  
      /**
       * customizable toast
       * @param message
       */
  
  
      private void toastMessage (String message){
          Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
      }
  
  
  
  
      // GO TO SECOND PAGE (index.XML)
      //also add on " android:onClick="gotoHome" " in the xml with the name matching the void action.
  
      public void gotoHome (View view) {
          Intent intent = new Intent(this, index.class); // go to index.class (index.xml)
          startActivity(intent);
      }
  
  
  
  
  } // END CLASS
  </code></pre>
  
  <p><strong>DatabaseHelper.java</strong> </p>
  
  <pre><code>public class DatabaseHelper extends SQLiteOpenHelper {
  
      private static final String TAG = "DatabaseHelper";
  
      private static final String TABLE_NAME = "friends_table";
      private static final String COL1 = "firstname";
      private static final String COL2 = "lastname";
      private static final String COL3 = "malefemale";
      private static final String COL4 = "age";
      private static final String COL5 = "address";
  
  
      public DatabaseHelper (Context context) {
          super(context, TABLE_NAME, null, 1);
      }
  
  
      @Override
      public void onCreate(SQLiteDatabase db) {
          String createTable = "CREATE TABLE " + TABLE_NAME + " (ID TEXT PRIMARY KEY AUTOINCREMENT, " +
                  COL2 +" TEXT)";
          db.execSQL(createTable);
      }
  
      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
          db.execSQL("DROP IF TABLE EXISTS" + TABLE_NAME);
          onCreate(db);
      }
  
      public boolean addData(String item) {
          SQLiteDatabase db = this.getWritableDatabase();
          ContentValues contentValues = new ContentValues();
          //
          // 
          contentValues.put(COL1, item);
          contentValues.put(COL2, item);
          contentValues.put(COL3, item);
          contentValues.put(COL4, item);
          contentValues.put(COL5, item);
  
          Log.d(TAG, "addData: Adding " + item + " to " + TABLE_NAME);
  
          long result = db.insert(TABLE_NAME, null, contentValues);
  
          //if date as inserted inorrectly it will return -1
          if (result == 1)
              return false;
          else {
              return true;
          }
  
  
      } //end addData
  
      /**
       * Returns al data from DB
       */
  
      public Cursor getData() {
          SQLiteDatabase db = this.getWritableDatabase();
          String query = "SELECT * FROM " + TABLE_NAME;
          Cursor data = db.rawQuery(query, null);
          return data;
      }
  
      /**
       * Returns ID that matches the name
       * searches the DB and returns the ID associated with that name
       */
  
      public Cursor getItemID(String name) {
          SQLiteDatabase db = this.getWritableDatabase();
          String query = "SELECT " + COL1 + " FROM " + TABLE_NAME +  // SELECT ID FROM DB
                  " WHERE " + COL2 + " = '" + name + "'";             // WHERE THE LAST NAME = NAME SELECTED
  
          Cursor data = db.rawQuery(query, null);
          return data;
      }
  
      /**
       * UPDATES THE NAME
       *
       * UPDATE TABLE &gt; SET LASTNNAME(COL2) = newName = WHERE id = id in Question = AND LASTNAME(COL2) = oldName (was previously) &gt;
       */
      public void updateName (String newName, int id, String oldName) {
          SQLiteDatabase db = this.getWritableDatabase();
          String query = "UPDATE " + TABLE_NAME + " SET " + COL2 +
                  " = '" + newName + "' WHERE " + COL1 + " = '" + id + "'" +
                  " AND " + COL2 + " = '" + oldName + "'";
  
          //LOGS THE NEW NAME
          Log.d(TAG, "updateName: query: " + query);
          Log.d(TAG, "updateName: Setting name to " + newName); // NEW NAME CHANGING IT TO
          db.execSQL(query); // EXECUTE QUERY
      }
  
      /**
       * DELETE FROM DATABASE
       * &gt;&gt;&gt; DELETE FROM TABLE WHERE id = id passed AND name = name passed
       *
       */
          public void deleteName(int id, String name){
              SQLiteDatabase db = this.getWritableDatabase();
              String query = "DELETE FROM " + TABLE_NAME + " WHERE "
                      + COL1 + " = '" + id + "'" +
                      " AND " + COL2 + " = '" + name + "'";
  
              Log.d(TAG, "deleteName: query: " + query);
              Log.d(TAG, "deleteName: Deleting " + name + " from database.");
              db.execSQL(query); // EXECUTE QUERY
          }
  
  
  }
  </code></pre>
  
  <p>EDIT: this is the error found in logcat:</p>
  
  <pre><code>        09-09 22:42:05.216 5846-5846/com.example.chris.mobileappsassignment E/SQLiteDatabase: Error inserting address=cht lastname=cht firstname=cht age=cht malefemale=cht
                                                                                        android.database.sqlite.SQLiteException: table friends_table has no column named address (code 1): , while compiling: INSERT INTO friends_table(address,lastname,firstname,age,malefemale) VALUES (?,?,?,?,?)
                                                                                            at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                                                                                            at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
                                                                                            at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)
                                                                                            at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                                                                                            at android.database.sqlite.SQLiteProgram.&lt;init&gt;(SQLiteProgram.java:58)
                                                                                            at android.database.sqlite.SQLiteStatement.&lt;init&gt;(SQLiteStatement.java:31)
                                                                                            at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
                                                                                            at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)
                                                                                            at com.example.chris.mobileappsassignment.DatabaseHelper.addData(DatabaseHelper.java:57)  // ERROR SEEMS TO APPEAR HERE
                                                                                            at com.example.chris.mobileappsassignment.friends.AddData(friends.java:168) // ERROR SEEMS TO APPEAR HERE
                                                                                            at com.example.chris.mobileappsassignment.friends$1.onClick(friends.java:73) // ERROR SEEMS TO APPEAR HERE
                                                                                            at android.view.View.performClick(View.java:5198)
                                                                                            at android.view.View$PerformClick.run(View.java:21147)
                                                                                            at android.os.Handler.handleCallback(Handler.java:739)
                                                                                            at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                            at android.os.Looper.loop(Looper.java:148)
                                                                                            at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
  09-09 22:42:05.218 5846-5846/com.example.chris.mobileappsassignment D/DatabaseHelper: addData: Adding ffff to friends_table
  09-09 22:42:05.218 5846-5846/com.example.chris.mobileappsassignment E/SQLiteLog: (1) table friends_table has no column named address
  09-09 22:42:05.219 5846-5846/com.example.chris.mobileappsassignment E/SQLiteDatabase: Error inserting address=ffff lastname=ffff firstname=ffff age=ffff malefemale=ffff
                                                                                        android.database.sqlite.SQLiteException: table friends_table has no column named address (code 1): , while compiling: INSERT INTO friends_table(address,lastname,firstname,age,malefemale) VALUES (?,?,?,?,?)
                                                                                            at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                                                                                            at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
                                                                                            at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)
                                                                                            at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                                                                                            at android.database.sqlite.SQLiteProgram.&lt;init&gt;(SQLiteProgram.java:58)
                                                                                            at android.database.sqlite.SQLiteStatement.&lt;init&gt;(SQLiteStatement.java:31)
                                                                                            at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
                                                                                            at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)
                                                                                            at com.example.chris.mobileappsassignment.DatabaseHelper.addData(DatabaseHelper.java:57) // ERROR SEEMS TO APPEAR HERE
                                                                                            at com.example.chris.mobileappsassignment.friends.AddData(friends.java:168) // ERROR SEEMS TO APPEAR HERE
                                                                                            at com.example.chris.mobileappsassignment.friends$1.onClick(friends.java:80) // ERROR SEEMS TO APPEAR HERE
                                                                                            at android.view.View.performClick(View.java:5198)
                                                                                            at android.view.View$PerformClick.run(View.java:21147)
                                                                                            at android.os.Handler.handleCallback(Handler.java:739)
                                                                                            at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                            at android.os.Looper.loop(Looper.java:148)
                                                                                            at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
  09-09 22:42:05.223 5846-5846/com.example.chris.mobileappsassignment D/DatabaseHelper: addData: Adding Name to friends_table
  09-09 22:42:05.223 5846-5846/com.example.chris.mobileappsassignment E/SQLiteLog: (1) table friends_table has no column named address
  09-09 22:42:05.223 5846-5846/com.example.chris.mobileappsassignment E/SQLiteDatabase: Error inserting address=Name lastname=Name firstname=Name age=Name malefemale=Name
                                                                                        android.database.sqlite.SQLiteException: table friends_table has no column named address (code 1): , while compiling: INSERT INTO friends_table(address,lastname,firstname,age,malefemale) VALUES (?,?,?,?,?)
                                                                                            at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                                                                                            at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
                                                                                            at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)
                                                                                            at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                                                                                            at android.database.sqlite.SQLiteProgram.&lt;init&gt;(SQLiteProgram.java:58)
                                                                                            at android.database.sqlite.SQLiteStatement.&lt;init&gt;(SQLiteStatement.java:31)
                                                                                            at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
                                                                                            at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)
                                                                                            at com.example.chris.mobileappsassignment.DatabaseHelper.addData(DatabaseHelper.java:57) // ERROR SEEMS TO APPEAR HERE
                                                                                            at com.example.chris.mobileappsassignment.friends.AddData(friends.java:168) // ERROR SEEMS TO APPEAR HERE
                                                                                            at com.example.chris.mobileappsassignment.friends$1.onClick(friends.java:87) // ERROR SEEMS TO APPEAR HERE
                                                                                            at android.view.View.performClick(View.java:5198)
                                                                                            at android.view.View$PerformClick.run(View.java:21147)
                                                                                            at android.os.Handler.handleCallback(Handler.java:739)
                                                                                            at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                            at android.os.Looper.loop(Looper.java:148)
                                                                                            at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
  09-09 22:42:05.224 5846-5846/com.example.chris.mobileappsassignment D/DatabaseHelper: addData: Adding Name to friends_table
  09-09 22:42:05.224 5846-5846/com.example.chris.mobileappsassignment E/SQLiteLog: (1) table friends_table has no column named address
  09-09 22:42:05.224 5846-5846/com.example.chris.mobileappsassignment E/SQLiteDatabase: Error inserting address=Name lastname=Name firstname=Name age=Name malefemale=Name
                                                                                        android.database.sqlite.SQLiteException: table friends_table has no column named address (code 1): , while compiling: INSERT INTO friends_table(address,lastname,firstname,age,malefemale) VALUES (?,?,?,?,?)
                                                                                            at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                                                                                            at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
                                                                                            at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)
                                                                                            at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                                                                                            at android.database.sqlite.SQLiteProgram.&lt;init&gt;(SQLiteProgram.java:58)
                                                                                            at android.database.sqlite.SQLiteStatement.&lt;init&gt;(SQLiteStatement.java:31)
                                                                                            at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
                                                                                            at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)
                                                                                            at com.example.chris.mobileappsassignment.DatabaseHelper.addData(DatabaseHelper.java:57) //ERROR SEEMS TO APPEAR HERE
                                                                                            at com.example.chris.mobileappsassignment.friends.AddData(friends.java:168) //ERROR SEEMS TO APPEAR HERE
                                                                                            at com.example.chris.mobileappsassignment.friends$1.onClick(friends.java:93) //ERROR SEEMS TO APPEAR HERE
                                                                                            at android.view.View.performClick(View.java:5198)
                                                                                            at android.view.View$PerformClick.run(View.java:21147)
                                                                                            at android.os.Handler.handleCallback(Handler.java:739)
                                                                                            at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                            at android.os.Looper.loop(Looper.java:148)
                                                                                            at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
  </code></pre>
  
  <p>It would be great if the answer would be put as simply as possible as I am still a beginner!</p>
  
  <p>Any help will be greatly appreciated! Thanks!</p>