这段代码有什么问题吗?我想使用条形码查询数据,它显示游标索引超出界限异常。
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;
}
中没有结果Cursor
。你应该检查什么moveToFirst()
正在返回(很可能false
)。你也应该使用moveToNext()
, not moveToFirst()
。还要注意你没有检查ss
范围。这可能会导致 SQL 注入漏洞。您应该使用参数。我也认为你可以在你的方法中使用单个返回。
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};
final String args = new String[1];
args[0] = ss;
Cursor c = ourDatabase.query(TABLE_NAME, column, Pro_Barcode + " = ?" , args, null, null, null);
String ret = null;
if(c.moveToNext()) {
ret = c.getString(0);
}
return ret;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)