Android Error in Select Query
-
11-12-2019 - |
Question
Below is my database method which is causing the issue:
public Cursor fetchRecord(String pin,String category)throws SQLException
{
Cursor c=mDb.query(DATABASE_TABLE, new String[]
{KEY_ROWID,KEY_NAME,KEY_PHONE,KEY_CATEGORY,KEY_PINCODE},
KEY_PINCODE + "= ?" + "AND" + KEY_CATEGORY + "= ?",
new String[]{pin,category}, null, null, KEY_NAME);
if (c!= null) {
c.moveToFirst();
}
return c;
}
it gets force close whenever i give input and click go button in the first activity the log cat says:
Caused by: android.database.sqlite.SQLiteException: near "ANDcategory": syntax error: , while compiling: SELECT _id, name, phone, category, pincode FROM details WHERE pincode= ?ANDcategory= ? ORDER BY name
Solution
There must be space between the pincode & category, in your where clause.
simply replace
"AND"
with
" AND "
public Cursor fetchRecord(String pin,String category)throws SQLException
{
Cursor c=mDb.query(DATABASE_TABLE, new String[]
{KEY_ROWID,KEY_NAME,KEY_PHONE,KEY_CATEGORY,KEY_PINCODE},
KEY_PINCODE + "= ?" +
" AND " + KEY_CATEGORY + "= ?",new String[]{pin,category}, null, null, KEY_NAME);
if (c!= null) {
c.moveToFirst();
}
return c;
}
OTHER TIPS
read the exception, it tells you everything you need to know:
syntax error: , while compiling: SELECT _id, name, phone, category, pincode FROM details WHERE pincode= ?ANDcategory= ? ORDER BY name
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow