I have created a SQLite DB in Android Studio which stores 4 values. When I 
attempt to create another column to store another piece of data, the data 
is not being store in DB. Only the first 4 columns I created are being used.
Here's the code for reference: 

DATABASEHELPERCLASS: 

package com.example.rahul.referenceproject;

/**
 * Created by Rahul on 9/8/17.
 */

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


public class DatabaseHelper extends SQLiteOpenHelper {
    public static final String DATABASE_NAME = "fueldata.db";
    public static final String TABLE_NAME = "fuel_data";
    public static final String COL_1 = "ID";
    public static final String COL_2 = "NAME";//PRICE
    public static final String COL_3 = "SURNAME";//GALLLONS
    public static final String COL_4 = "MARKS";//TOTALAMT
    //public static final String COL_5 = "STATION";



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

    @Override
    public void onCreate(SQLiteDatabase db) {
        //db.execSQL("create table " + TABLE_NAME + COL_1 + " INTEGER PRIMARY 
KEY AUTOINCREMENT , " + COL_2 + " TEXT," +
              //  COL_3 + " TEXT, "+ COL_4 + " INTEGER, " + COL_5 + " TEXT" + 
")");//STATION TEXT)");
        db.execSQL("create table " + TABLE_NAME +" (ID INTEGER PRIMARY KEY 
AUTOINCREMENT , PRICE TEXT , GALLONS TEXT , TOTAL TEXT )"); //STATION 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 price,String gallons,String ttlamt){ //, 
String station) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL_2,price);
        contentValues.put(COL_3,gallons);
        contentValues.put(COL_4,ttlamt);
        //contentValues.put(COL_5,station);
        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;
    }

    public boolean updateData(String id,String price,String gallons,String 
ttlamt){//, String station) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL_1,id);
        contentValues.put(COL_2,price);
        contentValues.put(COL_3,gallons);
        contentValues.put(COL_4,ttlamt);
        //contentValues.put(COL_5,station);
        db.update(TABLE_NAME, contentValues, "ID = ?",new String[] { id });
        return true;
    }

    public Integer deleteData (String id) {
        SQLiteDatabase db = this.getWritableDatabase();
        return db.delete(TABLE_NAME, "ID = ?",new String[] {id});
    }
}



MAIN ACTIVITY

package com.example.rahul.referenceproject;

import android.app.AlertDialog;
import android.database.Cursor;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {
    DatabaseHelper myDb;
    EditText editPrice,editGallons,editTotalAmt ,editTextId, editStation;
    Button btnAddData;
    Button btnviewAll;
    Button btnDelete;
    Button btnviewUpdate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myDb = new DatabaseHelper(this);

        editPrice = (EditText)findViewById(R.id.editText_price);
        editGallons = (EditText)findViewById(R.id.editText_gallons);
        editTotalAmt = (EditText)findViewById(R.id.editText_totalamt);
        //editTextId = (EditText)findViewById(R.id.editText_id);
        //editStation=(EditText) findViewById(R.id.editText_Station);
        btnAddData = (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();
    }

    public  void AddData() {
        btnAddData.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (editPrice.getText().toString().isEmpty());
                        {
                            editPrice.setError("Invalid price");
                        }
                        if 
(editPrice.getText().toString().contains("!@#$%^&*()_+?><:{}[];',./-="));
                        {
                            editPrice.setError("Please enter a valid price");
                        }
                        if 
(editPrice.getText().toString().contains("qwertyuiopasdfghjklzxcvbnm"));
                        {
                            editPrice.setError("Please enter a valid price");
                        }

                        if (editGallons.getText().toString().isEmpty());
                        {
                            editGallons.setError("Please enter the number of 
gallons before proceeding");
                        }
                        if (editGallons.getText().toString().length()==2)
                        {
                            editGallons.setError("Please enter the number of 
gallons before proceeding");
                        }
                        if 
(editGallons.getText().toString().contains("!@#$%^&*()_+?><:{}[];',./-="));
                        {
                            editGallons.setError("Please enter a valid number 
of gallons");
                        }

                        if (editTotalAmt.getText().toString().isEmpty());
                        {
                            editTotalAmt.setError("Please enter the total 
amount before proceeding");
                        }
                        if (editTotalAmt.getText().toString().length()==4)
                        {

                            editTotalAmt.setError("Please enter the total 
amount before proceeding");
                        }
                        if 
(editTotalAmt.getText().toString().contains("!@#$%^&*()_+?><:{}[];',./-="));
                        {
                            editTotalAmt.setError("Please enter a valid total 
amount");
                        }

                        boolean isInserted = 
myDb.insertData(editPrice.getText().toString(),
                                editGallons.getText().toString(),
                                editTotalAmt.getText().toString());//, 
editStation.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();

                        editPrice.getText().clear();
                        editGallons.getText().clear();
                        editTotalAmt.getText().clear();
                    }
                }
        );
    }

    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("Price :"+ res.getString(1)+"\n");
                            buffer.append("Gallons :"+ res.getString(2)+"\n");
                            buffer.append("Ttlamt :"+ res.getString(3)+"\n");
                            //buffer.append("Station :"+ 
res.getString(4)+"\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();
    }


}

-- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/android-developers/e82293ec-15d4-4334-9edc-c1e4c0d95ad1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to