SQLite 求某列的和 SUM()

前端之家收集整理的这篇文章主要介绍了SQLite 求某列的和 SUM()前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

关键代码: 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 ?",month_year+"%"});
				  break;
			case 1:
				  cursor = db.rawQuery("select sum(money) from money where user_id=? and kind_name=? and keep_date like ?",year+"%"});
				  break;
		}
        if (cursor!=null)
        {
                if (cursor.moveToFirst())
                {
                        do
                        {
                                sum=cursor.getInt(0);
                        } while (cursor.moveToNext());
                }
        }
        return sum;
	}
原文链接:https://www.f2er.com/sqlite/200540.html

猜你在找的Sqlite相关文章