Android SQLite。在更新版本时添加列

时间:2015-11-25 22:12:59

标签: android sqlite

请帮我解决这个问题。这是我更新的代码版本。我正在为此更新版本添加一列。我想我已尽我所能。

现在,我上传了应用程序。我可以更新数据,但它没有显示。应用程序正在崩溃。

这是我的主要活动类

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();

}

这是我的助手课程

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});

}

我需要帮助。正在加载数据。但是,为了显示,App正在崩溃。

这是我的XML文件

<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"/>

1 个答案:

答案 0 :(得分:1)

请替换:

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

使用:

<?xml version="1.0" encoding="utf-8"?>
在您的父布局中

,因为不支持1.5