Search code examples
androidsqliteopenhelpercontent-values

ContentValues only entering last value into database


I'm still fairly new to Android Studio and I have looked at this question in different forums but I haven't exactly found one that pertains to my problem specifically (or I'm not experienced enough to recognize it at least.)

I have created an activity where some one enters 3 different words into 3 different EditText views and those 3 separate values are supposed to be inserted into "column 1" or "COL_1" of the database upon creation, and of course create another intent and move to the next activity after checking/proving that the values were inserted to the database.

My problem is that ONLY THE THIRD word is entered as a row (or record) into the database and not the first 2. Sorry like I said I am novice, what am I missing?:

EDIT TEXT ACTIVITY

package com.example.jonathan.om11;

import android.content.Intent;
import android.support.annotation.Nullable;
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 ContributionsActivity extends AppCompatActivity {

    Button goToMainPageBtn;
    ContribDatabase contribDatabase;
    EditText contribution1,contribution2,contribution3;

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

        contribDatabase = new ContribDatabase(this);
        contribution1 = (EditText) findViewById(R.id.contribution1);
        contribution2 = (EditText) findViewById(R.id.contribution2);
        contribution3 = (EditText) findViewById(R.id.contribution3);

        goToMainPageBtn = (Button) findViewById(R.id.goToMainPageBtn);
        goToMainPageBtn.setOnClickListener(new View.OnClickListener(){
            public void onClick (View v){
                boolean isInserted = contribDatabase.insertData(
                        contribution1.getText().toString(),
                        contribution2.getText().toString(),
                        contribution3.getText().toString());
                Toast.makeText(ContributionsActivity.this, "Thank you for contributing, enjoy!", Toast.LENGTH_SHORT).show();
                if(isInserted == true) {
                    toContribPage(v);
                } else {
                    Toast.makeText(ContributionsActivity.this, "Oops! Contributions did not save, try again please!", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
    public void toContribPage(View v) {
        Intent i = new Intent("android.intent.action.TabbedView");
        startActivity(i);
    }
}

SQLITEOPENHELPER CLASS

package com.example.jonathan.om11;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Created by Jonathan on 2/10/18.
 */

public class ContribDatabase extends  SQLiteOpenHelper {

    public static final String DATABASE_NAME = "Contributions.db";
    public static final String TABLE_NAME = "Contributions_table";
//    public static final String COL_1 = "ID";
    public static final String COL_1 = "CONTRIBUTIONS";

    public ContribDatabase(Context context) {
        super(context, DATABASE_NAME, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
//        db.execSQL("create table " + TABLE_NAME +" (ID INTEGER PRIMARY KEY AUTOINCREMENT, CONTRIBUTIONS TEXT)");
          db.execSQL("create table " + TABLE_NAME +" (CONTRIBUTIONS TEXT)");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }

    public boolean insertData(String contribution1, String contribution2, String contribution3) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues =  new ContentValues();
        contentValues.put(COL_1,contribution1);
        contentValues.put(COL_1,contribution2);
        contentValues.put(COL_1, contribution3);

        //Check for if data is inserted or not
        long result = db.insert(TABLE_NAME,null,contentValues);
        if(result == -1) {
            return false;
        } else {
            return true;
        }
    }

//    public Cursor getAllData() {
//        SQLiteDatabase db = this.getWritableDatabase();
//        Cursor res = db.rawQuery("select * from " + TABLE_NAME, null);
//        return res;
//    }
}

Solution

  • There is a problem in your code. The method insertData has some problems. Replace your code with below one

    public boolean insertData(String contribution1, String contribution2, String contribution3) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues =  new ContentValues();
        ContentValues contentValues2 =  new ContentValues();
        ContentValues contentValues3 =  new ContentValues();
        contentValues.put(COL_1,contribution1);
        contentValues2.put(COL_1,contribution2);
        contentValues3.put(COL_1, contribution3);
    
        //Check for if data is inserted or not
        long result = db.insert(TABLE_NAME,null,contentValues);
        long result2 = db.insert(TABLE_NAME,null,contentValues2);
        long result3 = db.insert(TABLE_NAME,null,contentValues3);
        if(result == -1 || result2 == -1 || result3 == -1) {
            return false;
        } else {
            return true;
        }
    }
    

    It was happening because rather inserting 3 different values you were mapping different values in a same object. Hope that helps.