Android query SQLIte syntax error

I receive data from an Activity in which I create a SQL query, but I'm getting syntax error in string. Thanks for help

String sql = "SELECT _Id,Date,cas,N,E,Altro FROM Ge WHERE Ea>0 AND cas LIKE "+c_ricevuta+" AND date LIKE '"+anno_ricevuto+"-%' ORDER BY data DESC";

here syntax error: cas LIKE "+c_ricevuta+"

Answers


c_ricevuta should likely be in single quotes '':

String sql = "SELECT _Id,Date,cas,N,E,Altro FROM Ge WHERE Ea>0 AND cas LIKE '"+c_ricevuta+"' AND date LIKE '"+anno_ricevuto+"-%' ORDER BY data DESC";

Note that using string params in your query like this is prone to both syntax errors and SQL injection. Have a look at ? literal placeholders instead, e.g. the selectionArgs in query functions.


just in case, I'd place spaces between the word and the "+". Plus, you're not using quotes to surround c_ricevuta


Need Your Help

NSError localizedDescription always returns english error message

ios iphone ipad nserror

Why does an NSError * error object with NSURLErrorDomain always return the error.localizedDescription in english ?

viewForHeaderInSection disappears when scrolling

ios objective-c iphone uitableview cocoa-touch

I am using a custom header view in my UITableView, but as soon as i begin scrolling down the header disappears instead of staying in place like with the default headers. And it's not scrolling off ...