gpt4 book ai didi

android - db.rawQuery SELECT SUM 中的游标返回 null : how to catch?

转载 作者:行者123 更新时间:2023-11-29 22:58:29 25 4
gpt4 key购买 nike

我正在运行以下查询,

Cursor c = db.rawQuery("SELECT SUM(surveycount) FROM surveyDB WHERE species = " + "'" + szSpecies + "' AND location = " + "'" + szLocation + "'", null); 

创建表时将数据库列“surveycount”声明为 INT。下面的代码工作正常,除非没有返回具有 szSpecies 和 szLocation 目标字符串值的记录,此时 szSum 显示为 null。

if (c.moveToFirst()) {
szSum = c.getString(0);
c.close();}
else{
//do nothing
Toast.makeText(getActivity().getApplicationContext(),"It was NULL",Toast.LENGTH_LONG).show();
}
Toast.makeText(getActivity().getApplicationContext(),"Surveycount SUM = "+szSum+" .",Toast.LENGTH_LONG).show();

如何捕获这个错误的光标输出?

最佳答案

首先检查游标是否不为空,然后查看cursor.getcount()是否大于0,然后尝试从游标中检索值。

更新后的代码如下:

Cursor c = db.rawQuery("SELECT SUM(surveycount) FROM surveyDB WHERE species = " + "'" + szSpecies + "' AND location = " + "'" + szLocation + "'", null); 
szSum="";

if(c!=null && c.getCount()>0)
{
c.moveToFirst()
szSum = c.getString(0);
c.close();
} else{
//do nothing
Toast.makeText(getActivity().getApplicationContext(),"It was NULL",Toast.LENGTH_LONG).show();
}

if(!szSum.equals(""))
Toast.makeText(getActivity().getApplicationContext(),"Surveycount SUM = "+szSum+" .",Toast.LENGTH_LONG).show();

getCount():返回游标中的行数

关于android - db.rawQuery SELECT SUM 中的游标返回 null : how to catch?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28616395/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com