关键代码: cursor = db.rawQuery("select sum(money) from money where user_id=? and kind_name=? and keep_date like ?"
,new String[]{userId,kindName,month_year+"%"});
取值: if (cursor.moveToFirst())
{
do
{
sum=cursor.getInt(0);
} while (cursor.moveToNext());
}
/**
* 根据年月(or年)、用户ID、类型返回消费金额
* @param userId
* @param kindName
* @return
*/
public int findSumOfKind(String userId,String kindName,String month_year,String year)
{
int sum=0;
int i=0;
Cursor cursor = null ;
db = helper.getWritableDatabase();
if(month_year==null)
{
i=1;
}
switch(i)
{
case 0:
cursor = db.rawQuery("select sum(money) from money where user_id=? and kind_name=? and keep_date like ?",new String[]{userId,kindName,month_year+"%"});
break;
case 1:
cursor = db.rawQuery("select sum(money) from money where user_id=? and kind_name=? and keep_date like ?",new String[]{userId,kindName,year+"%"});
break;
}
if (cursor!=null)
{
if (cursor.moveToFirst())
{
do
{
sum=cursor.getInt(0);
} while (cursor.moveToNext());
}
}
return sum;
}