There is a database with cities. I'm trying to check if there is a city in the database, but I always get res == 0. If you create a query that displays all cities in logcat, then all cities are correctly displayed in it. Here is the actual code:

public byte checkWord(String city) { if (used.contains(city)) return ALREADY_USED; // ! city = "Рим"; // ! String[] args = new String[] { "city=" + city }; Cursor c = db.rawQuery("SELECT COUNT(*) FROM cities WHERE ?", args); if (c.moveToFirst()) { int res = c.getInt(0); if (res == 1) { c.close(); return OK; } } return CITY_NOT_FOUND; } 

What could be the reason?

PS Rome is exactly in the database)

    1 answer 1

    Try this:

     String[] args = new String[] { city }; Cursor c = db.rawQuery("SELECT COUNT(*) FROM cities WHERE city=?", args); 
    • Thank you very much. I tried it before, but I crashed with java.lang.IllegalArgumentException: Cannot bind the argument at index 1 because the index is out of range. The statement has 0 parameters. Maybe then I confused something. - Ziens