数据库代码主要是查找

package com.example.Utils.database;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.example.Utils.fragment.FloatUtils; import java.util.ArrayList;
import java.util.List; /*
* 负责管理数据库的类
* 主要对于表当中的内容进行操作,增删改查
* */
public class DBManager { private static SQLiteDatabase db;
/* 初始化数据库对象*/
public static void initDB(Context context){
DBOpenHelper helper = new DBOpenHelper(context); //得到帮助类对象
db = helper.getWritableDatabase(); //得到数据库对象
} /**
* 读取数据库当中的数据,写入内存集合里
* kind :表示收入或者支出
* */
public static List<TypeBean> getTypeList(int kind){
List<TypeBean>list = new ArrayList<>();
//读取typetb表当中的数据
String sql = "select * from typetb where kind = "+kind;
Cursor cursor = db.rawQuery(sql, null);
// 循环读取游标内容,存储到对象当中
while (cursor.moveToNext()) {
@SuppressLint("Range") String typename = cursor.getString(cursor.getColumnIndex("typename"));
@SuppressLint("Range") int imageId = cursor.getInt(cursor.getColumnIndex("imageId"));
@SuppressLint("Range") int sImageId = cursor.getInt(cursor.getColumnIndex("sImageId"));
@SuppressLint("Range") int kind1 = cursor.getInt(cursor.getColumnIndex("kind"));
@SuppressLint("Range") int id = cursor.getInt(cursor.getColumnIndex("id"));
TypeBean typeBean = new TypeBean(id, typename, imageId, sImageId, kind);
list.add(typeBean);
}
return list;
} /*
* 向记账表当中插入一条元素
* */
public static void insertItemToAccounttb(AccountBean bean){
ContentValues values = new ContentValues();
values.put("typename",bean.getTypename());
values.put("sImageId",bean.getsImageId());
values.put("beizhu",bean.getBeizhu());
values.put("money",bean.getMoney());
values.put("time",bean.getTime());
values.put("year",bean.getYear());
values.put("month",bean.getMonth());
values.put("day",bean.getDay());
values.put("kind",bean.getKind());
db.insert("accounttb",null,values);
} public static List<AccountBean>getAccountListOneDayFromAccounttb(int year,int month,int day){
List<AccountBean>list = new ArrayList<>();
String sql = "select * from accounttb where year=? and month=? and day=? order by id desc";
Cursor cursor = db.rawQuery(sql, new String[]{year + "", month + "", day + ""});
//遍历符合要求的每一行数据
while (cursor.moveToNext()) {
@SuppressLint("Range") int id = cursor.getInt(cursor.getColumnIndex("id"));
@SuppressLint("Range") String typename = cursor.getString(cursor.getColumnIndex("typename"));
@SuppressLint("Range") String beizhu = cursor.getString(cursor.getColumnIndex("beizhu"));
@SuppressLint("Range") String time = cursor.getString(cursor.getColumnIndex("time"));
@SuppressLint("Range") int sImageId = cursor.getInt(cursor.getColumnIndex("sImageId"));
@SuppressLint("Range") int kind = cursor.getInt(cursor.getColumnIndex("kind"));
@SuppressLint("Range") float money = cursor.getFloat(cursor.getColumnIndex("money"));
AccountBean accountBean = new AccountBean(id, typename, sImageId, beizhu, money, time, year, month, day, kind);
list.add(accountBean);
}
return list;
} public static List<AccountBean>getAccountListOneMonthFromAccounttb(int year,int month){
List<AccountBean>list = new ArrayList<>();
String sql = "select * from accounttb where year=? and month=? order by id desc";
Cursor cursor = db.rawQuery(sql, new String[]{year + "", month + ""});
//遍历符合要求的每一行数据
while (cursor.moveToNext()) {
@SuppressLint("Range") int id = cursor.getInt(cursor.getColumnIndex("id"));
@SuppressLint("Range") String typename = cursor.getString(cursor.getColumnIndex("typename"));
@SuppressLint("Range") String beizhu = cursor.getString(cursor.getColumnIndex("beizhu"));
@SuppressLint("Range") String time = cursor.getString(cursor.getColumnIndex("time"));
@SuppressLint("Range") int sImageId = cursor.getInt(cursor.getColumnIndex("sImageId"));
@SuppressLint("Range") int kind = cursor.getInt(cursor.getColumnIndex("kind"));
@SuppressLint("Range") float money = cursor.getFloat(cursor.getColumnIndex("money"));
@SuppressLint("Range") int day = cursor.getInt(cursor.getColumnIndex("day"));
AccountBean accountBean = new AccountBean(id, typename, sImageId, beizhu, money, time, year, month, day, kind);
list.add(accountBean);
}
return list;
} public static float getSumMoneyOneDay(int year,int month,int day,int kind){
float total = 0.0f;
String sql = "select sum(money) from accounttb where year=? and month=? and day=? and kind=?";
Cursor cursor = db.rawQuery(sql, new String[]{year + "", month + "", day + "", kind + ""});
// 遍历
if (cursor.moveToFirst()) {
@SuppressLint("Range") float money = cursor.getFloat(cursor.getColumnIndex("sum(money)"));
total = money;
}
return total;
} public static float getSumMoneyOneMonth(int year,int month,int kind){
float total = 0.0f;
String sql = "select sum(money) from accounttb where year=? and month=? and kind=?";
Cursor cursor = db.rawQuery(sql, new String[]{year + "", month + "", kind + ""});
// 遍历
if (cursor.moveToFirst()) {
@SuppressLint("Range") float money = cursor.getFloat(cursor.getColumnIndex("sum(money)"));
total = money;
}
return total;
} public static int getCountItemOneMonth(int year,int month,int kind){
int total = 0;
String sql = "select count(money) from accounttb where year=? and month=? and kind=?";
Cursor cursor = db.rawQuery(sql, new String[]{year + "", month + "", kind + ""});
if (cursor.moveToFirst()) {
@SuppressLint("Range") int count = cursor.getInt(cursor.getColumnIndex("count(money)"));
total = count;
}
return total;
} public static float getSumMoneyOneYear(int year,int kind){
float total = 0.0f;
String sql = "select sum(money) from accounttb where year=? and kind=?";
Cursor cursor = db.rawQuery(sql, new String[]{year + "", kind + ""});
// 遍历
if (cursor.moveToFirst()) {
@SuppressLint("Range") float money = cursor.getFloat(cursor.getColumnIndex("sum(money)"));
total = money;
}
return total;
} public static int deleteItemFromAccounttbById(int id){
int i = db.delete("accounttb", "id=?", new String[]{id + ""});
return i;
} public static List<AccountBean>getAccountListByRemarkFromAccounttb(String beizhu){
List<AccountBean>list = new ArrayList<>();
String sql = "select * from accounttb where beizhu like '%"+beizhu+"%'";
Cursor cursor = db.rawQuery(sql, null);
while (cursor.moveToNext()) {
@SuppressLint("Range") int id = cursor.getInt(cursor.getColumnIndex("id"));
@SuppressLint("Range") String typename = cursor.getString(cursor.getColumnIndex("typename"));
@SuppressLint("Range") String bz = cursor.getString(cursor.getColumnIndex("beizhu"));
@SuppressLint("Range") String time = cursor.getString(cursor.getColumnIndex("time"));
@SuppressLint("Range") int sImageId = cursor.getInt(cursor.getColumnIndex("sImageId"));
@SuppressLint("Range") int kind = cursor.getInt(cursor.getColumnIndex("kind"));
@SuppressLint("Range") float money = cursor.getFloat(cursor.getColumnIndex("money"));
@SuppressLint("Range") int year = cursor.getInt(cursor.getColumnIndex("year"));
@SuppressLint("Range") int month = cursor.getInt(cursor.getColumnIndex("month"));
@SuppressLint("Range") int day = cursor.getInt(cursor.getColumnIndex("day"));
AccountBean accountBean = new AccountBean(id, typename, sImageId, bz, money, time, year, month, day, kind);
list.add(accountBean);
}
return list;
} public static List<Integer>getYearListFromAccounttb(){
List<Integer>list = new ArrayList<>();
String sql = "select distinct(year) from accounttb order by year asc";
Cursor cursor = db.rawQuery(sql, null);
while (cursor.moveToNext()) {
@SuppressLint("Range") int year = cursor.getInt(cursor.getColumnIndex("year"));
list.add(year);
}
return list;
} public static void deleteAllAccount(){
String sql = "delete from accounttb";
db.execSQL(sql);
} public static List<ChartItemBean>getChartListFromAccounttb(int year, int month, int kind){
List<ChartItemBean>list = new ArrayList<>();
float sumMoneyOneMonth = getSumMoneyOneMonth(year, month, kind); //求出支出或者收入总钱数
String sql = "select typename,sImageId,sum(money)as total from accounttb where year=? and month=? and kind=? group by typename " +
"order by total desc";
Cursor cursor = db.rawQuery(sql, new String[]{year + "", month + "", kind + ""});
while (cursor.moveToNext()) {
@SuppressLint("Range") int sImageId = cursor.getInt(cursor.getColumnIndex("sImageId"));
@SuppressLint("Range") String typename = cursor.getString(cursor.getColumnIndex("typename"));
@SuppressLint("Range") float total = cursor.getFloat(cursor.getColumnIndex("total"));
//计算所占百分比 total /sumMonth
float ratio = FloatUtils.div(total,sumMoneyOneMonth);
ChartItemBean bean = new ChartItemBean(sImageId, typename, ratio, total);
list.add(bean);
}
return list;
} public static float getMaxMoneyOneDayInMonth(int year,int month,int kind){
String sql = "select sum(money) from accounttb where year=? and month=? and kind=? group by day order by sum(money) desc";
Cursor cursor = db.rawQuery(sql, new String[]{year + "", month + "", kind + ""});
if (cursor.moveToFirst()) {
@SuppressLint("Range") float money = cursor.getFloat(cursor.getColumnIndex("sum(money)"));
return money;
}
return 0;
} public static List<BarChartItemBean>getSumMoneyOneDayInMonth(int year, int month, int kind){
String sql = "select day,sum(money) from accounttb where year=? and month=? and kind=? group by day";
Cursor cursor = db.rawQuery(sql, new String[]{year + "", month + "", kind + ""});
List<BarChartItemBean>list = new ArrayList<>();
while (cursor.moveToNext()) {
@SuppressLint("Range") int day = cursor.getInt(cursor.getColumnIndex("day"));
@SuppressLint("Range") float smoney = cursor.getFloat(cursor.getColumnIndex("sum(money)"));
BarChartItemBean itemBean = new BarChartItemBean(year, month, day, smoney);
list.add(itemBean);
}
return list;
} }

Android开发-数据库代码编写的更多相关文章

  1. Android开发数据库之第三方ORM框架(GreenDao)

    移动APP追求追求功能实现的同一时候,用户体验很重要.開始APP的同一时候.要时刻的切换开发人员的角色,如你开发的时候.是 站在APP的开发角色,处于生产者的位置:当你測试的时候.你应该把自己放在用户 ...

  2. Android开发数据库三层应用-DataSnap

    Android开发数据库三层应用-DataSnap http://www.2ccc.com/news/Html/?1517.html 核心提示:我觉得Delphi最强大的的功能之一就是开发数据库三层应 ...

  3. 36个Android开发常用代码片段

    //36个Android开发常用代码片段 //拨打电话 public static void call(Context context, String phoneNumber) { context.s ...

  4. Android 开发有用代码积累

    Android开发需求变化快,开发周期要求尽量短,接下来一系列文章从实际使用出发总结一些常用的代码片段,便于查找,也为后来人提供一份参考. 1.获取Manifest的基本信息(升级页面和软件关于页面一 ...

  5. 〖Android〗OK6410a的Android HAL层代码编写笔记

    一.编写LED灯的Linux驱动程序代码 之所以使用存在HAL层,是为了保护对硬件驱动过程的逻辑与原理: 所以,残留在Linux驱动层的代码,只保留了基本的读写操作,而不含有关键的逻辑思维: 1. l ...

  6. android开发--数据库(更新或者降低版本)

    Andoird的SQLiteOpenHelper类中有一个onUpgrade方法. 1. 帮助文档里说的"数据库升级"是指什么? 你开发了一个应用,当前是1.0版本.该程序用到了数 ...

  7. Android开发常用代码片段

    拨打电话 public static void call(Context context, String phoneNumber) { context.startActivity( new Inten ...

  8. Android开发模板代码(一)——简单打开图库选择照片

    首先,先贴上样本代码 //检查权限 public void checkPermission() { if (ContextCompat.checkSelfPermission(this, Manife ...

  9. Android开发中代码下面出现波浪线问题

    在Android Studio中写代码时,经常会在一个英文单词的下面出现波浪线,这是因为系统检测到你的这个英文单词不符合规范,如下所示: 解决办法:选中这个单词,点击鼠标右键,点击:Spelling ...

随机推荐

  1. 【LeetCode】358. Rearrange String k Distance Apart 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/rearrang ...

  2. 【LeetCode】851. Loud and Rich 解题报告(Python)

    [LeetCode]851. Loud and Rich 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:// ...

  3. 【LeetCode】804. Unique Morse Code Words 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述: 题目大意 解题方法 set + map set + 字典 日期 题目地 ...

  4. 突破技术限制,实现Web端静默打印

    作为Web开发的同僚们,估计都有一个共同的烦恼,Web端为什么不能够像 CS端那样直接打印预览?直接移除掉打印预览界面不就可以了? 真实情况是Web端受限于浏览器的权限,无法直接访问打印机等本机资源. ...

  5. 洛谷——P1980 [NOIP2013 普及组] 计数问题

    题目描述 试计算在区间 11 到 nn的所有整数中,数字x(0 ≤ x ≤ 9)x(0≤x≤9)共出现了多少次?例如,在 11到1111中,即在 1,2,3,4,5,6,7,8,9,10,111,2, ...

  6. ADVERSARIAL EXAMPLES IN THE PHYSICAL WORLD

    目录 概 主要内容 least likely class adv. 实验1 l.l.c. adv.的效用 实验二 Alexey Kurakin, Ian J. Goodfellow, Samy Ben ...

  7. vmware虚拟IOS系统

    安装虚拟机     --以管理员的身份运行

  8. IM2603设计资料 Type-C拓展坞电源管理芯片

    应用于Type-C拓展坞外围集成Buck变换器的电源管理芯片 IM2603 IM2603 概述 用于带有集成降压转换器的 Type-C 外围应用的电源管理 IC IM2603 是一款主要用于 Type ...

  9. 乌龟NOI

    题目描述 一只乌龟由于智商低下,它只会向左或向右走,不过它会遵循主人小h的指令:F(向前走一步),T(掉头). 现在小h给出一串指令,由于小h有高超的计算能力,他可以马上知道乌龟最后走到哪里.为了难倒 ...

  10. linux(CentOS7) 之 zookeeper 下载及安装

    下载 搜索apache ,进入apache官网(https://www.apache.org/)下载 选择downloads 下的distribution 点击archive site 找到zooke ...