mercredi 25 novembre 2015

Android SQLite. Adding a column while updating version

Please help me with this. This is my updated version of code. I'm adding a column to this updated version. I think I've done all I can.

Now, I uploaded the App. I can update the data, but it is not being displayed. App is crashing.

This is my Main Activity Class

SQLiteHelper sqLiteHelper;
EditText etParentGroup, etCompanyMake, etModelName, etDebutYear, etCubicCentimeters;
Button bnSaveTheData, bnDisplayTheData, bnUpdateTheData, bnDeleteTheData;

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

    sqLiteHelper = new SQLiteHelper(this);

    etParentGroup = (EditText) findViewById(R.id.etParentGroup);
    etCompanyMake = (EditText) findViewById(R.id.etCompanyMake);
    etModelName = (EditText) findViewById(R.id.etModelName);
    etDebutYear = (EditText) findViewById(R.id.etDebutYear);
    etCubicCentimeters = (EditText) findViewById(R.id.etCubicCentimeters);

    bnSaveTheData = (Button) findViewById(R.id.bnSaveTheData);
    bnDisplayTheData = (Button) findViewById(R.id.bnDisplayTheData);
    bnUpdateTheData = (Button) findViewById(R.id.bnUpdateTheData);
    bnDeleteTheData = (Button) findViewById(R.id.bnDeleteTheData);

    AddData();
    ViewAll();
    UpdateData();
    deleteData();

}

private void AddData() {

    bnSaveTheData.setOnClickListener(

            new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    boolean isInserted = sqLiteHelper.insertData(
                            etParentGroup.getText().toString(),
                            etCompanyMake.getText().toString(),
                            etModelName.getText().toString(),
                            etDebutYear.getText().toString(),
                            etCubicCentimeters.getText().toString());

                    if (isInserted == true) {

                        Toast.makeText(MainSQLiteActivity.this, "Data is Inserted", Toast.LENGTH_SHORT).show();

                    } else {

                        Toast.makeText(MainSQLiteActivity.this, "Data isn't Inserted", Toast.LENGTH_SHORT).show();

                    }

                    etParentGroup.setText(null);
                    etCompanyMake.setText(null);
                    etModelName.setText(null);
                    etDebutYear.setText(null);
                    etCubicCentimeters.setText(null);

                }

            }

    );

}

public void ViewAll(){

    bnDisplayTheData.setOnClickListener(

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

                    Cursor res = sqLiteHelper.getAllData();

                    if (res.getCount() == 0){

                        ShowData("Error", "Data isn't found");
                        return;

                    }

                    StringBuffer stringBuffer = new StringBuffer();
                    while (res.moveToNext()){
                        stringBuffer.append("PARENT_GROUP: " + res.getString(0) + "\n");
                        stringBuffer.append("COMPANY_MAKE: " + res.getString(1) + "\n");
                        stringBuffer.append("MODEL_NAME: " + res.getString(2) + "\n");
                        stringBuffer.append("DEBUT_YEAR: " + res.getString(3) + "\n");
                        stringBuffer.append("CC: " + res.getString(4) + "\n\n");
                    }

                    ShowData("Data",stringBuffer.toString());

                }
            }

    );

}

public void UpdateData() {

    bnUpdateTheData.setOnClickListener(

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

                    boolean isUpdated = sqLiteHelper.updateData(
                            etParentGroup.getText().toString(),
                            etCompanyMake.getText().toString(),
                            etModelName.getText().toString(),
                            etDebutYear.getText().toString(),
                            etCubicCentimeters.getText().toString()
                    );

                    if (isUpdated == true){

                        Toast.makeText(MainSQLiteActivity.this, "Data is updated", Toast.LENGTH_SHORT).show();

                    }

                    else{

                        Toast.makeText(MainSQLiteActivity.this, "Data isn't updated", Toast.LENGTH_SHORT).show();

                    }

                }
            }

    );

}

public void deleteData(){

    bnDeleteTheData.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Integer deleteRows = sqLiteHelper.deleteData(etParentGroup.getText().toString());

            if (deleteRows > 0){

                Toast.makeText(MainSQLiteActivity.this, "Data is deleted", Toast.LENGTH_SHORT).show();

            }

            else {

                Toast.makeText(MainSQLiteActivity.this, "Data isn't deleted", Toast.LENGTH_SHORT).show();

            }
        }
    });

}

public void ShowData(String title, String message){

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setCancelable(true);
    builder.setTitle(title);
    builder.setMessage(message);
    builder.show();

}

This is my Helper class

public static final String DATABASE_NAME = "Cars.db";
public static final int VERSION = 2;
public static final String TABLE_NAME = "Cars_table";
public static final String COL_1 = "PARENT_GROUP";
public static final String COL_2 = "COMPANY_MAKE";
public static final String COL_3 = "MODEL_NAME";
public static final String COL_4 = "DEBUT_YEAR";
public static final String COL_5 = "CUBIC_CENTIMETERS";


public SQLiteHelper(Context context) {

    super(context, DATABASE_NAME, null, VERSION);

}

@Override
public void onCreate(SQLiteDatabase db) {

     String queryTable =  "CREATE TABLE " + TABLE_NAME + " (" +
                    COL_1 + " PRIMARY KEY, " +
                    COL_2 + " TEXT NOT NULL, " +
                    COL_3 + " TEXT NOT NULL, " +
                    COL_4 + " INTEGER, " +
                    COL_5 + " INTEGER);";

     db.execSQL(queryTable);

}

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

    switch (oldVersion) {
        case 1:
            db.execSQL(TABLE_NAME);


        case 2:
            db.execSQL("ALTER TABLE Cars ADD COLUMN COL_5 INTEGER");

    }

    /**String upgradeQuery = "ALTER TABLE Cars ADD COLUMN COL_5 INTEGER";
    if (oldVersion == 1 && newVersion == 1.1)
        db.execSQL(upgradeQuery);*/

}

public boolean insertData(String PARENT_GROUP, String COMPANY_MAKE, String MODEL_NAME, String DEBUT_YEAR, String CUBIC_CENTIMETERS) {

    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL_1, PARENT_GROUP);
    contentValues.put(COL_2, COMPANY_MAKE);
    contentValues.put(COL_3, MODEL_NAME);
    contentValues.put(COL_4, DEBUT_YEAR);
    contentValues.put(COL_5, CUBIC_CENTIMETERS);
    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 PARENT_GROUP, String COMPANY_MAKE, String MODEL_NAME, String DEBUT_YEAR, String CUBIC_CENTIMETERS) {

    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL_1, PARENT_GROUP);
    contentValues.put(COL_2, COMPANY_MAKE);
    contentValues.put(COL_3, MODEL_NAME);
    contentValues.put(COL_4, DEBUT_YEAR);
    contentValues.put(COL_5, CUBIC_CENTIMETERS);
    db.update(TABLE_NAME, contentValues, "PARENT_GROUP = ?", new String[] {PARENT_GROUP});
    return true;

}

public Integer deleteData(String PARENT_GROUP){

    SQLiteDatabase db = this.getWritableDatabase();
    return db.delete(TABLE_NAME, "PARENT_GROUP = ?", new String[] {PARENT_GROUP});

}

I need help with this. Data is being loaded. But, for displaying, App is crashing.

Aucun commentaire:

Enregistrer un commentaire