0

I'm making the app that stores a list of the user's purchases in a SQLite database in android. A problem I'm having is I can't delete any of the entries. This is the error message I am getting: android.database.sqlite.SQLiteException: no such column: purchaseId (code 1): , while compiling: DELETE FROM purchases WHERE purchaseId='0'. It's not finding the purchaseId column. What can I do?

here's my relevant code:

In this function I store a list of the database contents in an arraylist called purchaseList. Then I iterate through all of items, passing the id to my deletePurchase method:

public void clearHistory(){
        purchaseList = dbTools.getAllPurchases();
        int size = purchaseList.size();
        for(int id = 0; id < size; id++) {
            String idString = String.valueOf(id);
            dbTools.deletePurchase(idString);

        }

    }

In this function I create the database. purchaseId is an INTEGER PRIMARY KEY, which autoincrements:

public void onCreate(SQLiteDatabase database) {
        String query = "CREATE TABLE purchases ( purchaseId INTEGER PRIMARY KEY, datePurchased TEXT, " +
                "storeName TEXT, price TEXT)";


        database.execSQL(query);
}

This function is where I think the problem is. I know purchaseId is an integer, but is this line correct: String deleteQuery = "DELETE FROM purchases WHERE purchaseId='"+ id +"'";?

public void deletePurchase(String id) {
        SQLiteDatabase database = this.getWritableDatabase();

        String deleteQuery = "DELETE FROM purchases WHERE purchaseId='"+ id +"'";   //***line of interest***
        database.execSQL(deleteQuery);

    }

I think there is an easy fix because the deleteQuery works if I use another column like storeName, which is a STRING

Any help?

user3195991
  • 79
  • 1
  • 1
  • 7

2 Answers2

2

try like this: for integer type ' are not necessary.

String deleteQuery = "DELETE FROM purchases WHERE purchaseId="+ id ;   
Rathan Kumar
  • 2,567
  • 2
  • 17
  • 24
  • Still getting similar error: android.database.sqlite.SQLiteException: no such column: purchaseId (code 1): , while compiling: DELETE FROM purchases WHERE purchaseId=0 – user3195991 Dec 29 '14 at 05:35
  • 1
    once uninstall and run the application, and auto increment starts from 1 – Rathan Kumar Dec 29 '14 at 05:38
1

try this remove single quote(')

  String deleteQuery = "DELETE FROM purchases WHERE purchaseId="+ id; //Remove single quote

Your purchaseId column type is integer.

M D
  • 47,665
  • 9
  • 93
  • 114
  • I am getting a similar error unfortunately: android.database.sqlite.SQLiteException: no such column: purchaseId (code 1): , while compiling: DELETE FROM purchases WHERE purchaseId=0 – user3195991 Dec 29 '14 at 05:36