SQLite数据库未在应用程序中更新

时间:2016-07-06 08:57:51

标签: android-sqlite

我正在开发一个应用程序。修改数据时,将显示先前的数据,而不是更新的数据。这是ModifyActivity.java文件:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class ModifyCountryActivity extends Activity implements OnClickListener {

private EditText titleText, dateText, timeText;
private Button updateBtn, deleteBtn;

public Calendars calendars;


private DatabaseHelper dbHelper;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setTitle("Modify Record");

    setContentView(R.layout.activity_modify_record);

    dbHelper = new DatabaseHelper(this);
    calendars = new Calendars();

    titleText = (EditText) findViewById(R.id.title_edittext_modify);
    timeText = (EditText) findViewById(R.id.time_edittext_modify);
    dateText = (EditText) findViewById(R.id.date_edittext_modify);

    updateBtn = (Button) findViewById(R.id.btn_update);
    deleteBtn = (Button) findViewById(R.id.btn_delete);

    Intent intent = getIntent();
    String title = intent.getStringExtra("title");
    String time = intent.getStringExtra("time");
    String date = intent.getStringExtra("date");

    titleText.setText(title);
    timeText.setText(time);
    dateText.setText(date);

    updateBtn.setOnClickListener(this);
    deleteBtn.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.btn_update:
            titleText.setText(titleText.getText().toString() + " ");
            timeText.setText(dateText.getText().toString() + " ");
            dateText.setText(timeText.getText().toString() + " ");

            calendars.set_remindertitle(titleText.getText().toString() + " ");

            calendars.set_reminderdate(dateText.getText().toString() + " ");
            calendars.set_remindertime(timeText.getText().toString() + " ");

            dbHelper.update(calendars);

            this.returnHome();
            break;

        case R.id.btn_delete:
            dbHelper.delete(calendars);
            this.returnHome();
            break;
    }
}

public void returnHome() {
    Intent home_intent = new Intent(getApplicationContext(), CountryListActivity.class)
            .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(home_intent);
   }
}

数据库不会更新。它再次显示以前的数据。这是数据库类:

public class DatabaseHelper extends SQLiteOpenHelper {
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "calendar.db";
public static final String TABLE_REMINDER = "reminder";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_REMINDER_TITLE = "_remindertitle";
public static final String COLUMN_REMINDER_DESCRIPTION = "_reminderdescription";
public static final String COLUMN_REMINDER_DATE = "_reminderdate";
public static final String COLUMN_REMINDER_TIME = "_remindertime";
public static final String COLUMN_REMINDER_REPEAT = "_reminderrepeat";
public static final String COLUMN_REMINDER_SNOOZE = "_remindersnooze";
SQLiteDatabase database;
// Database Information
Class<? extends DatabaseHelper> context = getClass();


DatabaseHelper dbHelper;


// Creating table query
public void onCreate(SQLiteDatabase db) {
    String query = "CREATE TABLE " + TABLE_REMINDER + " (" +
            COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
            COLUMN_REMINDER_DATE + " TEXT, " + COLUMN_REMINDER_TIME + " TEXT, " + COLUMN_REMINDER_TITLE + " TEXT, "
            + COLUMN_REMINDER_DESCRIPTION + " TEXT, " + COLUMN_REMINDER_REPEAT + " TEXT, " + COLUMN_REMINDER_SNOOZE + " TEXT " + ");";
    Log.i("Query", query);


    db.execSQL(query);
}

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


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

public ArrayList<Calendars> databaseToArrayList() {

    ArrayList<Calendars> arrayList = new ArrayList();

    SQLiteDatabase db = getWritableDatabase();
    String query = "SELECT * FROM " + TABLE_REMINDER;
    Cursor c = db.rawQuery(query, null);
    c.moveToFirst();
    while (!c.isAfterLast()) {
        if (c.getString(c.getColumnIndex("_reminderdate")) != null) {

            Calendars calendars = new Calendars();
            calendars.set_reminderdate(c.getString(c.getColumnIndex(COLUMN_REMINDER_DATE)));
            calendars.set_remindertime(c.getString(c.getColumnIndex(COLUMN_REMINDER_TIME)));
            calendars.set_remindertitle(c.getString(c.getColumnIndex(COLUMN_REMINDER_TITLE)));
            calendars.set_reminderdescription(c.getString(c.getColumnIndex(COLUMN_REMINDER_DESCRIPTION)));
            calendars.set_reminderrepeat(c.getString(c.getColumnIndex(COLUMN_REMINDER_REPEAT)));
            calendars.set_remindersnooze(c.getString(c.getColumnIndex(COLUMN_REMINDER_SNOOZE)));

            arrayList.add(calendars);
        }
        c.moveToNext();
    }
    c.close();
    db.close();
    return arrayList;

}

public void remove(String id) {
    String string = String.valueOf(id);
    SQLiteDatabase database = getReadableDatabase();
    database.execSQL("DELETE FROM " + TABLE_REMINDER + " WHERE _id = '" + string + "'");
}

public void addReminder(Calendars calendars) {
    ContentValues contentValues = new ContentValues();
    contentValues.put(COLUMN_REMINDER_DATE, calendars.get_reminderdate());
    contentValues.put(COLUMN_REMINDER_TIME, calendars.get_remindertime());
    contentValues.put(COLUMN_REMINDER_TITLE, calendars.get_remindertitle());
    contentValues.put(COLUMN_REMINDER_DESCRIPTION, calendars.get_reminderdescription());
    contentValues.put(COLUMN_REMINDER_REPEAT, calendars.get_reminderrepeat());
    contentValues.put(COLUMN_REMINDER_SNOOZE, calendars.get_remindersnooze());
    SQLiteDatabase database = getReadableDatabase();
    database.insert(TABLE_REMINDER, null, contentValues);
    Log.i("insData", "the data has been inseted");

    database.close();

}


public Cursor fetch() {
    String[] columns = new String[]{COLUMN_ID, /*COLUMN_REMINDER_DATE, COLUMN_REMINDER_TIME, COLUMN_REMINDER_TITLE,*/
            COLUMN_REMINDER_DESCRIPTION, COLUMN_REMINDER_REPEAT, COLUMN_REMINDER_SNOOZE};

    SQLiteDatabase database = getReadableDatabase();
    Cursor cursor = database.query(TABLE_REMINDER, columns, null, null, null, null, null);
    if (cursor != null)

    {
        cursor.moveToFirst();
    }

    return cursor;
}


public int update(Calendars calendars) {
    ContentValues contentValues = new ContentValues();
    contentValues.put(COLUMN_REMINDER_DATE, calendars.get_reminderdate());
    contentValues.put(COLUMN_REMINDER_TIME, calendars.get_remindertime());
    contentValues.put(COLUMN_REMINDER_TITLE, calendars.get_remindertitle());/*
    contentValues.put(COLUMN_REMINDER_DESCRIPTION, calendars.get_reminderdescription());
    contentValues.put(COLUMN_REMINDER_REPEAT, calendars.get_reminderrepeat());
    contentValues.put(COLUMN_REMINDER_SNOOZE, calendars.get_remindersnooze());*/
    SQLiteDatabase database = getReadableDatabase();
    int i = database.update(TABLE_REMINDER, contentValues, COLUMN_ID + " = " + calendars.get_id(), null);
    database.close();
    return i;

}


public void delete(Calendars calendars) {
    database = getReadableDatabase();

    database.delete(TABLE_REMINDER, COLUMN_ID + "=" + calendars.get_id(), null);
  }
}

我相信更新按钮应该可以正常工作。我是Android新手,不知道如何解决这个问题。关于如何解决它的任何建议?

1 个答案:

答案 0 :(得分:0)

如果你的更新功能返回int,那么在onClick函数中,而不是输入:

dbHelper.update(calendars);

您需要输入:

int update = dbHelper.update(calendars);

或者:

if (dbHelper.update(calendars) > 0) {
    // do something here
}

我会推荐后一种选择。看你怎么走。