7

Hey all Im a newbie Androidian, and would love some help with this?

android.database.sqlite.SQLiteException: no such column: asd: , while compiling: DELETE FROM labels WHERE name=asd

this is the error am facing, and here's the code:

this is the method in the DBhelper:

/**
 * Delete a label table
 * */
public void deleteLabel(String label) {
    SQLiteDatabase db = this.getWritableDatabase();

    // ContentValues values = new ContentValues();
    // values.remove(label);

    // Deleting Row
    db.delete(TABLE_LABELS, KEY_NAME + "=" + label, null);
    db.close(); // Closing database connection
}

and here's the main Activity code that invoke the method:

// for spinner onItemListener
// and here is what label is

final String label = parent.getItemAtPosition(position).toString();

Button dialogDeletelButton = (Button) dialog
                .findViewById(R.id.deleteButton);
        dialogDeletelButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // database handler
                DatabaseHandler db = new DatabaseHandler(
                        getApplicationContext());

                // inserting new label into database
                db.deleteLabel(label);

                // Hiding the keyboard
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(inputLabel.getWindowToken(), 0);

                // loading spinner without the deleted data
                loadSpinnerData();

            }
        });
Tayseer
  • 491
  • 1
  • 5
  • 16

1 Answers1

14

You almost certainly need to quote 'asd' (i.e., the label variable in your code). If it's quoted, it's a string to compare against the name column.

If it's unquoted, SQL just treats it as another column name.

You could do this in your activity with:

db.deleteLabel ("'" + label + "'");

but it may be cleaner to change the helper function:

db.delete (TABLE_LABELS, KEY_NAME + "='" + label + "'", null);

since it looks like you may want to do something with the unquoted label at some point in there (the ContentValues stuff which is currently commented out).

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953