Reputation: 9351
A very simple SQL statement is not working, any ideas?
rawQuery("SELECT * FROM " + CITY_TABLE + ";", columns);
error from stacktrace
android.database.sqlite.SQLiteBindOrColumnIndexOutOfRangeException:
bind or column index out of range: handle 0x19b098006-
the rest of the code
public ArrayList<String> getCitiesFromCountry(int countryCode){
String[] columns = new String[]{CITY_NAME};
ArrayList<String> cityNames = new ArrayList<String>();
Cursor cursor = sqLiteDatabase.rawQuery("SELECT * FROM " + CITY_TABLE + ";", columns);
if (cursor != null){
while(cursor.moveToNext()){
cityNames.add(cursor.getString(cursor.getColumnIndex(CITY_NAME)));
....
}
...
}
return cityNames;
}
database constants
public static final String WORLD_DATABASE = "world_database";
public static final String COUNTRY_TABLE = "country_table";
public static final String CITY_TABLE = "city_table";
public static final int DATABASE_VERSION = 1;
public static final String _ID = "_id";
public static final String _ID2 = "_id2";
public static final String COUNTRY_NAME = "country_name";
public static final String CITY_NAME = "city_name";
Upvotes: 0
Views: 1010
Reputation: 203
Try to get the result using either of these two methods
while(cursor.moveToNext()){
cityNames.add(cursor.getString(Column_Name);
or
cityNames.add(cursor.getString(ColumnIndex);
}
and in the raw query you can remove the second argument very well and replace it as null. db.rawQuery("Select * from "+Table_Name,null);
Upvotes: 0
Reputation: 3752
Why do you need columns
in the statement? You don't need it:
rawQuery("SELECT * FROM " + CITY_TABLE, null);
If you want to pass selection arguments then you should use it. For example, if you want to select only those rows with city_name = 'New York'
rawQuery("SELECT * FROM " + CITY_TABLE + " WHERE " + CITY_NAME + " = ?", new String[] {"New York"});
See this for more info.
Upvotes: 2
Reputation: 130
You should try removing the ';' sign from the select query That's what might be causing the problem,also check the number of column's the number of column's might be the cause as well.
Upvotes: 1
Reputation: 5472
If you want to retrieve all columns you dont require that second param in rawquery
rawQuery("SELECT * FROM " + CITY_TABLE , null);
And if you want to retrieve specific rows then use like this...
rawQuery("SELECT COL1, COL2 FROM " + CITY_TABLE + " WHERE COL1 = ? AND COL2 = ?", new String[] {"CRUSADER", "CITY"});
Upvotes: 2
Reputation: 723
For raw query, you do not put the columns for the 2nd paramter, as those are for where arguments. ie having a query like
db.rawQuery("SELECT * FROM " + CITY_TABLE + " WHERE city_name = ?;", new String[]{"Moon"});
If you want to have columns coming from a string array. You can use.
db.query(CITY_TABLE, COLUMNS, null, null, null, null, null);
This will select the city_table, for the columns
Upvotes: 2