0

android.database.sqlite.SQLiteException: no such column: pos (code 1 SQLITE_ERROR): , while compiling: DELETE FROM pre_table WHERE _id = pos;

I try few things. But it doesn't work.

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 public class PreviewDBHelper extends 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);

}

}

I want remove data when I long click my list

Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30
Kathleen
  • 25
  • 1
  • 5

2 Answers2

0

You are trying to remove "pos" so, I guess your problem is on this line

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

It should be

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

So you are deleting the position.

A cool idea you can use this method also to be more clear when deleting stuff

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

Edit

But still it doesn't remove on screen

Perhaps you can use myAdapter.notifyItemRemoved(position); Or just remove it from your list and then use the notifyDataSetChanged() method from your adapter.

Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148
0

As mentioned in another answers, that delete query is wrong and should be updated to:

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

As you said, however, the list is not being updated. This is happening because you must delete the item in the Adapter as well.

So, I suggests following changes:

@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();
    }
}
guipivoto
  • 18,327
  • 9
  • 60
  • 75