从数据库中删除数据

时间:2018-03-07 14:17:37

标签: android sqlite android-sqlite

我无法从我的数据库中删除数据,我真的需要一些帮助,问题是在这个例子中我想删除第3行中的数据,但是它没有做任何事情。

尝试从数据库中删除数据(我的ListData类)

public void onClick (DialogInterface dialog, int item) {
                        // delete
                        Cursor c = Dataedit.myDB.getListContents();
                        ArrayList<Integer> arrID = new ArrayList<Integer>();
                        while (c.moveToNext()) {
                            arrID.add(c.getInt(0));
                        }
                        showDialogDelete(arrID.get(position));
                    }
                }

-

private void showDialogDelete(final int id) {
    final AlertDialog.Builder dialogDelete = new AlertDialog.Builder(ListData.this);

    dialogDelete.setTitle("Warning!!");
    dialogDelete.setMessage("Do you want to delete?");
    dialogDelete.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            int id2 = 2;

            try {
                myDB.delete_byID(id2);
                updateList();
                Toast.makeText(getApplicationContext(), "Izdzēsts!", Toast.LENGTH_SHORT).show();
            } catch (Exception e) {
                Log.e("error", e.getMessage());
            }
        }
    });

    dialogDelete.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });
    dialogDelete.show();
}

我的数据库,我存储所有数据,需要什么

public class Database extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "users.db";
public static final String TABLE_NAME = "users_data";
public static final String COL1 = "ID";
public static final String COL2 = "FIRSTNAME";
public static final String COL3 = "LASTNAME";
public static final String COL4 = "QRNUMBER";
SQLiteDatabase database;


public Database(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, " +
            " FIRSTNAME TEXT, LASTNAME TEXT, QRNUMBER TEXT)";
    db.execSQL(createTable);
}

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

public boolean addData(String fName, String lName, String fQRnum) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL2, fName);
    contentValues.put(COL3, lName);
    contentValues.put(COL4, fQRnum);

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

//query for 1 week repeats
public Cursor getListContents() {
    SQLiteDatabase db = this.getWritableDatabase();
    String select = "SELECT * FROM ";
    Cursor data = db.rawQuery(select + TABLE_NAME, null);
    return data;
}

public void delete_byID(int id){
    database.delete(TABLE_NAME, COL1 + "=" +id, null);
}

1 个答案:

答案 0 :(得分:0)

您似乎没有将 database 设置为任何值,因此它将为空。

你有很多选择可以解决这个问题: -

您可以在Database类的构造函数中初始化 database ,例如: -

public Database(Context context) {
    super(context, DATABASE_NAME, null, 1);
    database = this.getWriteableDatabase();
}

您可以使用delete_byID方法对其进行初始化,例如: -

public void delete_byID(int id){
    database = this.getWriteableDatabase();
    database.delete(TABLE_NAME, COL1 + "=" +id, null);
}

您无法像在其他方法中那样使用它,并且delete_byID更改了: -

public void delete_byID(int id){
    SQLiteDatabase db = this.getWriteableDatabase();
    db.delete(TABLE_NAME, COL1 + "=" +id, null);
}

您可以直接使用SQLiteDatabase返回的getWriteableDatabase,例如: -

public void delete_byID(int id){
    this.getWriteableDatabase.delete(TABLE_NAME, COL1 + "=" +id, null);
}