SQLite数据库不删除条目

时间:2019-02-13 18:31:34

标签: java sqlite android-sqlite

在我的Android应用程序中,我创建一个Sqlite表,其中包含按ID排序的信息。因此,我添加了一种方法,通过将id传递给方法来再次删除条目。当我从另一个类调用该方法时,在delete方法中会收到正确的ID,但是由于某些原因,该条目不会被删除。删除方法是错误的还是我设置了错误的方法?

public class DatabaseHelper extends SQLiteOpenHelper {

    public static final String DATABASE_NAME = "mylist.db";
    public static final String TABLE_NAME = "mylist_data";
    public static final String COL1 = "ID";
    public static final String COL2 = "NAME";
    public static final String COL3 = "TIME";
    public static final String COL4 = "SKILL";


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

    @Override
    public void onCreate(SQLiteDatabase db) {
        String createTable = "CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
                " NAME TEXT," + " TIME TEXT," + " SKILL TEXT)";
        db.execSQL(createTable);
    }

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

    public boolean addData(String name, String time, String skill) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL2, name);
        contentValues.put(COL3, time);
        contentValues.put(COL4, skill);
        Log.d("skill", skill);

        long result = db.insert(TABLE_NAME, null, contentValues);

        //if date as inserted incorrectly it will return -1
        if (result == -1) {
            return false;
        } else {
            return true;
        }
    }

    public void deleteTask(int rowId)
    {
        SQLiteDatabase db = this.getWritableDatabase();

        Log.d("delete id", String.valueOf(rowId));

        String query = "DELETE FROM " + TABLE_NAME + " WHERE "
                + COL1 + " = '" + rowId + "'";

         db.execSQL(query);
    }


    public Cursor getListContents(){
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor data = db.rawQuery("SELECT ID, NAME, TIME, SKILL FROM " + TABLE_NAME, null);
        return data;
    }
}

0 个答案:

没有答案