Mukund
Mukund

Reputation: 1105

error occurs when updating a table in sqlite

String updateQuery = "UPDATE Bookdetails SET lastchapter = " + test + " WHERE bookpath=" +sentFilename; 
db.execSQL(updateQuery);

Error:

03-04 13:36:23.997: I/System.out(9722): android.database.sqlite.SQLiteException: 
near "/": syntax error: , while compiling: UPDATE Bookdetails SET lastchapter = 
mukund WHERE bookpath=/mnt/sdcard/Download/What's so great about the doctrine of 
 grace.epub errors happens

the error is posted above

My table contains the field id, bookpath and lastchapter, book path contains the values

 /mnt/sdcard/Download/What's so great about the doctrine of grace.epub  
  /mnt/sdcard/Download/1b4fdaac-f31d-41e8-9d15-26c9078d891f.epub 
  /mnt/sdcard/Download/Commentary on Romans.epub

and lastchapter contains the values nothing nothing nothing

id contains 1 2 3

why is the error occurring at "/" there is no hash in my update query it is only there at string which stores bookpath? Is this an error?

Upvotes: 0

Views: 66

Answers (2)

Lucifer
Lucifer

Reputation: 29642

I believe your lastchapter & bookpath is of type String (TEXT). Hence when you are adding or updating it's value you should always use ' ( Single cot ) around it. Change your query to this,

String updateQuery = "UPDATE Bookdetails SET lastchapter ='" + test + "' WHERE bookpath='" +sentFilename + "'"; 
db.execSQL(updateQuery);

However, Direct Execution of SQL query is not advisable at developer.android.com hence you can use alternative way like below code,

String updateQuery = "UPDATE Bookdetails SET lastchapter=? WHERE bookpath=?";
db.execSQL(updateQuery, new String[] { test, sentFilename });

Upvotes: 1

laalto
laalto

Reputation: 152827

String literals in SQL need to be in '' quotes.

However, it's better to use ? placeholders for literals like this:

String updateQuery = "UPDATE Bookdetails SET lastchapter=? WHERE bookpath=?";
db.execSQL(updateQuery, new String[] { test, sentFilename });

Upvotes: 6

Related Questions