dimanche 28 décembre 2014

Issue with Android SQLite: 'No such column' error

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?


Aucun commentaire:

Enregistrer un commentaire