ShashankAC
ShashankAC

Reputation: 1078

Unable to solve a syntax error near WHERE in sqlite, android

I have a String with an SQLite statement that I would like to execute like this..

String query = "UPDATE Inventory SET ProductName =" 
+ product.getProductName() + ", InStock =" + product.getProductInStock()
+ ", CostPrice =" + product.getProductCostPrice() + ",SellingPrice =" 
+ product.getProductSellingPrice() + ", Description =" 
+ product.getProductDescription() + " WHERE rowid =" 
+ String.valueOf(Integer.parseInt(rowId) + 1);

db.execSQL(query);

I have a POJO class called Product. rowid is an inbuilt variable right ? I tried it in uppercase and it still didn't work.

I am getting the following error

E/SQLiteLog: (1) near "WHERE": syntax error

Upvotes: 0

Views: 76

Answers (1)

forpas
forpas

Reputation: 164069

In your code you are setting values to text columns without the use of single quotes.
This can be one of the problems of the code.
The recommended way of doing updates is by using ContentValues which is also sql-injection safe:

ContentValues cv = new ContentValues();
cv.put("ProductName", product.getProductName());
cv.put("InStock", product.getProductInStock());
cv.put("CostPrice", product.getProductCostPrice());
cv.put("SellingPrice", product.getProductSellingPrice());
cv.put("Description", product.getProductDescription());
int result = db.update("Inventory", cv, "rowid = ?", new String[] {String.valueOf(Integer.parseInt(rowId) + 1)});

The variable result will contain the number of rows updated.
I'm not sure about that value:

String.valueOf(Integer.parseInt(rowId) + 1)

but if you have tested it then fine.

Upvotes: 1

Related Questions