I am getting a Cursor Index out of bounds error after opening a
database . Please can any one tell me how to open an existing database in
sqllite - Android . I want to fire a select query on the database and
retrieve some information ?
public void getPatient(SQLiteDatabase db, String name) {
// TODO Auto-generated method stub
db=this.getReadableDatabase();
//db.open();
System.out.println("in cursooooooooorrrrrrrrr");
Cursor c = db.rawQuery("SELECT * from table_patient WHERE COL_Name"+"=?",
new String []{name});
//c.moveToFirst();
int index = c.getColumnIndex("COL_FirstName");
System.out.println("First Name : ----------- "+c.getString(index));
c.close();
}
do it like this:
int index = 0;
if(c.moveToFirst())
index = c.getColumnIndex("COL_FirstName");
You must uncomment c.moveToFirst().
Here an example:
public WhoisEntry[] getAllEntries(){
SQLiteDatabase db = getReadableDatabase();
String sql = "SELECT * FROM domains_events";
Cursor result = db.rawQuery(sql, null);
WhoisEntry[] resultArray = new WhoisEntry[result.getCount()];
int i=0;
result.moveToFirst();
while (i< result.getCount()){
long date = result.getLong(result.getColumnIndex("exp_date")) * 1000;
String domainName = result.getString(result.getColumnIndex("domainname"));
resultArray[i] = new WhoisEntry(domainName, new Date(date));
//resultArray[i].domainName =
//resultArray[i].expirationDate = result.getString(result.getColumnIndex("exp_date"));
String name = result.getString(result.getColumnIndex("domainname"));
i++;
}
return resultArray;
}