is there any thing wrong with this code, i want to query for data by using barcode and it show me that Cursor Index out of Bound exception .
public String getIdByBarcode(String ss) throws SQLException{
String[] column = new String[]{Pro_ID,Pro_Barcode, Pro_Name,Pro_NameKhmer, Pro_Quantity, Pro_Price, Pro_Description, Pro_Date};
Cursor c = ourDatabase.query(TABLE_NAME, column, Pro_Barcode + "= '" + ss + "' " , null, null, null, null);
if(c != null){
c.moveToFirst();
String id = c.getString(0);
Log.v(id, id + "Id" );
return id;
}
return null;
}
The literature of moveToFirst() method:
So your moveToFirst call is failing(because cusrsor has 0 elements) and that is the reason for crash.
Do this:
try this
No results in the
Cursor
. You should check whatmoveToFirst()
is returning (most likelyfalse
). Also you should usemoveToNext()
, notmoveToFirst()
. Also watch out that you're not checkingss
parameter. This could lead to SQL injection vulnerabilities. You should be using parameters. Also I think you can use a single return in your method.