0

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+"

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

3 Comments

I'd place spaces between the word and the "+". what for?
I've seen some errors pop up because of details like that. Plus it's a good practice to make your code more readable
it's a good practice, yes (but the line's length does come first to mind when looking at this code in terms of readability), but it does not cause error. never.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.