2016-01-22 75 views
-2

我在将查询结果转换为整数时遇到了一些麻烦。我将查询的结果返回到游标中,并试图在另一个语句中再次使用该值。我知道正在返回的值是一个整数,但我似乎不能提取到一个整数变量的值。我也尝试将该语句返回给int,但这也不起作用。光标到整数转换

查询

public Cursor getCode(String name) throws SQLException 
{ 
    return db.rawQuery("select recipe_code from recipes where recipe_name = " + "'"+name+"'" ,null); 
} 

诠释转换工作现在

Cursor code = adapter.getCode(string); 
    code.moveToFirst(); 
    int test = code.getInt(0); 
+0

调试时代码的价值是什么? – Lal

+0

很多不同的字符,当我做了toString()它说'android.database.sql.SQLiteCursor @ cf5778c' – Hayes121

+1

尝试如果'int i = code.getInt(0);'工作.. – Lal

回答

1

替换

int test = code.getInt(); 

int test = code.getInt(0); 

您没有将列索引作为参数传递给getInt(),因此代码无法工作。您应该指定要选择的列。

按照docs

公共抽象INT getInt(INT columnIndex)

返回请求的列作为int的值。

结果和此方法是否抛出异常当列 值为空,列类型是不是一个整数类型,或整数 值在该范围之外[Integer.MIN_VALUE的,是Integer.MAX_VALUE]是 实现定义。

参数 columnIndex目标列的从零开始的索引。

返回该列作为int的值。

+0

Thankyou .. @Hayes :) – Lal