user3013257
user3013257

Reputation:

How to write prepared statements for this query?

Hi i have written a join query for my app and its working. My next requirement is to convert that query to prepared statement where I am stuck.

"SELECT * FROM " + TableA.TABLE + " as d LEFT JOIN " + TableB.TABLE + " as c ON d."+ TableA.DAMAGECODE + "=c." + TableB.DAMAGECODE + " AND d."
+ TableA.INDEX + "=c." + TableB.DAMAGECODEINDEX + " AND d."
+ TableA.OBJECTTYPE + "=c." + TableB.OBJECTCLASS + " WHERE d."+ TableA.LEAF + " = '1' AND d." + TableA.OBJECTTYPE + " = ? AND " + "(d."+ TableA.DAMAGECODE + " LIKE ? OR d." + TableA.DAMAGETEXT + " LIKE ?) AND c." + TableB.CONSTRUCTIONSERIES + " = ? ORDER BY " + TableA.DAMAGETEXT;

cursor = db.rawQuery(sql,new String[]{String.valueOf(objectClass),"'%" + query + "%'","'%" + query + "%'",constructionSeries});

When i ran raw query i am getting results ,but when ran the above prepared statements i am getting cursor count always zero

Upvotes: 0

Views: 131

Answers (1)

AxelH
AxelH

Reputation: 14572

You don't need to add the ' yourself when you pass a String parameters, the PreparedStatement will manage those itself.

"'%" + query + "%'"

For the moment you have condition like

where columnA = "'%somethingToFind%'"

So unless you have a value in columnA like 'somethingToFindInColumnA' (note the quote at the begin and the end of that String). You will never get a result.

Remove those to get something like :

"%" + query + "%"

Full answer :

db.rawQuery(sql,new String[]{String.valueOf(objectClass),"'%" + query + "%'","'%" + query + "%'",constructionSeries});

Become :

db.rawQuery(sql,new String[]{String.valueOf(objectClass),"%" + query + "%","%" + query + "%",constructionSeries});

Upvotes: 1

Related Questions