Android游标索引超出绑定异常 [英] Android Cursor Index out of Bound Exception

查看:58
本文介绍了Android游标索引超出绑定异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码有什么问题吗,我想使用条形码查询数据,它向我显示游标索引超出绑定异常。

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;
}


推荐答案

<$中没有结果c $ c>光标。您应该检查返回的是 moveToFirst()(很可能是 false )。另外,您应该使用 moveToNext(),而不是 moveToFirst()。另请注意,您没有检查 ss 参数。这可能会导致SQL注入漏洞。您应该使用参数。我也认为您可以在方法中使用单个返回值。

No results in the Cursor. You should check what moveToFirst() is returning (most likely false). Also you should use moveToNext(), not moveToFirst(). Also watch out that you're not checking ss 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.

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;
}

这篇关于Android游标索引超出绑定异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆