Im a noob to android and I am trying to run a query on my database to return a single string value from my table. I have done this successfully in one query by selecting my row id using a long. Shown here:
public String getHotness(long l) throws SQLException{
// TODO Auto-generated method stub
String[] columns = new String[]{ KEY_ROWID, KEY_NAME, KEY_HOTNESS };
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_ROWID + "=" + l, null, null, null, null);
if (c != null){
c.moveToFirst();
String hotness = c.getString(2);
return hotness;
}
c.close();
return null;
}
But now i have to query another table using a string, but when i execute this query i get a SQLException.
public String getSingleQty(String aCoin) throws SQLException{
// TODO Auto-generated method stub
String[] columns = new String[]{ KEY_ROWID, KEY_NAME, KEY_QUANTITY, KEY_OUNCES, KEY_VALUE};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_NAME + "=" + aCoin, null, null, null, null); //<--this causes exception
String result = "";
if (c != null){
c.moveToFirst();
result= c.getString(2);
c.close();
return result;
}
c.close();
return null;
I don’t understand why this is occurring. I used the same format for both tables. Any help is greatly appreciated.
Since aCoin is being passed as a string to the query, it has to be wrapped by apostrophes. Corrected fragment of code:
or (if the syntax suit you better) try it that way: