如何删除数据库中的数据?

时间:2019-06-26 10:13:42

标签: android database sqlite

android.database.sqlite.SQLiteException:没有这样的列:pos(代码1 SQLITE_ERROR):,而在编译时:从pre_table中删除_id = pos;

我尝试一些事情。 但这不起作用。

enter code here
/* Main Activity */

// member
Context context;
ContentValues value = new ContentValues();
ListView listView;
PreviewDBHelper dbHelper;
PreviewDBManager previewDBManager;
Cursor cursor = null;
AlertDialog.Builder builder;
private ArrayList<Preview> list = null;
private newAdapter myAdapter;
private mood_dialog mCustomDialog;
private weather_dialog wCustomDialog;
EditText etTitle;
EditText etContent;


AdapterView.OnItemLongClickListener longClickListener
        = new AdapterView.OnItemLongClickListener() {
    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        // remove data
        final Integer selectedPos = position;

        SQLiteDatabase db = dbHelper.getWritableDatabase();
        String pos = list.get(selectedPos).toString();

        list.get(position);
        db.execSQL("DELETE FROM " + PreviewDBHelper.TABLE_NAME
                + " WHERE _id = pos;");
        Toast.makeText(getApplicationContext(), "삭제 되었습니다", Toast.LENGTH_LONG).show();
        dbHelper.close();
        myAdapter.notifyDataSetChanged();

        return true;
    }


};

// DBHelper 公共类PreviewDBHelper扩展了SQLiteOpenHelper {

final static String TAG = "PreviewDBHelper";

final static String DB_NAME = "preview.db";
public final static String TABLE_NAME = "pre_table";

public final static String COL_ID = "_id";
public final static String COL_TITLE = "title";
public final static String COL_WEATHER = "weather";

public PreviewDBHelper(Context context) {
    super(context, DB_NAME, null, 1);
}


@Override
public void onCreate(SQLiteDatabase db) {
    String sql = "CREATE TABLE " + TABLE_NAME + " (" + COL_ID + " integer primary key autoincrement, " +
            COL_TITLE + " TEXT, " + COL_WEATHER + " TEXT)";
    Log.d(TAG, sql);
    db.execSQL(sql);

}

}

我要在我长按列表时删除数据

2 个答案:

答案 0 :(得分:0)

您正在尝试删除“ pos”,所以我想您的问题在这行上

db.execSQL("DELETE FROM " + PreviewDBHelper.TABLE_NAME
            + " WHERE _id = pos;");

应该是

db.execSQL("DELETE FROM " + PreviewDBHelper.TABLE_NAME
            + " WHERE _id = " +pos);

所以您要删除职位。

一个不错的主意,您也可以使用此方法在删除内容时更加清楚

https://stackoverflow.com/a/17917422/4385913

编辑

  

但仍然不能在屏幕上删除

也许您可以使用myAdapter.notifyItemRemoved(position); 或者只是将其从列表中删除,然后使用适配器中的notifyDataSetChanged()方法。

答案 1 :(得分:0)

如另一个答案中所述,该删除查询是错误的,应更新为:

db.execSQL("DELETE FROM " + PreviewDBHelper.TABLE_NAME + " WHERE _id = " + pos);

但是,正如您所说,该列表未更新。之所以发生这种情况,是因为您还必须删除适配器中的项目。

因此,我建议进行以下更改:

@Override
public boolean onItemLongClick(....) {

    // Delete using db.delete. This way, you can confirm the number of rows deleted
    int rowsDeleted = db.delete(PreviewDBHelper.TABLE_NAME, "_id=?", new String[]{Integer.toString(pos)});

    if(rowsDeleted > 0) {
        // Remove the item from the list. Adapter render items according to the items in your list (and not according to the items in the database)
        list.remove(pos);

        // Then, notify the adapter that the list was changed
        myAdapter.notifyDataSetChanged();
    }
}