0

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.

This is my XML file

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/parent_group"
    android:id="@+id/etParentGroup"/>

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:layout_below="@+id/etParentGroup"
    android:hint="@string/company_make"
    android:id="@+id/etCompanyMake"/>

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:layout_below="@+id/etCompanyMake"
    android:hint="@string/model_name"
    android:id="@+id/etModelName"/>

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:layout_below="@+id/etModelName"
    android:inputType="number"
    android:hint="@string/debut_year"
    android:id="@+id/etDebutYear"/>

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:layout_below="@+id/etDebutYear"
    android:hint="@string/cubic_centimeters"
    android:inputType="number"
    android:id="@+id/etCubicCentimeters"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/save_the_data"
    android:id="@+id/bnSaveTheData"
    android:layout_marginTop="25dp"
    android:layout_below="@+id/etCubicCentimeters"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/display_the_data"
    android:id="@+id/bnDisplayTheData"
    android:layout_alignTop="@+id/bnSaveTheData"
    android:layout_alignParentRight="true"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/update_the_data"
    android:id="@+id/bnUpdateTheData"
    android:layout_below="@id/bnSaveTheData"
    android:layout_marginTop="25dp"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/delete_the_data"
    android:id="@+id/bnDeleteTheData"
    android:layout_alignTop="@+id/bnUpdateTheData"
    android:layout_alignParentRight="true"/>
9
  • Please copy the stack trace of the crash, highlighting the line of code causing it. Thanks! Commented Nov 25, 2015 at 22:17
  • I generated the APK file and loaded it in my mobile. Cause is unknown. Commented Nov 25, 2015 at 22:21
  • You said the app is crashing, so if you attach your device to the USB you should be able to get the logcat and so the stack trace of the crash. If you're using Android Studio or Eclipse, you can see logs directly from there. Commented Nov 25, 2015 at 22:24
  • It is not accepting XML version more than 1.0. I need to update the version and upgrade the data. "Error:XML version "1.5" is not supported, only XML 1.0 is supported." Commented Nov 25, 2015 at 22:28
  • That issue is not related to the database code but probably to a wrong XML layout file. Did you add any new layout? Please post the relevant XML of the view that should be displayed when the application crashes. Commented Nov 25, 2015 at 22:34

1 Answer 1

1

Please replace:

<?xml version="1.5" encoding="utf-8"?>

with:

<?xml version="1.0" encoding="utf-8"?>

in your parent layout, because 1.5 is not supported.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes sir. I understood that. What if I want to to update the Version?
You can't, you have to use version 1.0 for XML files in an Android project. Also, I think version 1.5 doesn't exist, but only 1.1. Anyway keep 1.0.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.