数据库代码主要是查找

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】778. Swim in Rising Water 水位上升的泳池中游泳(Python)

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

  2. 【LeetCode】309. Best Time to Buy and Sell Stock with Cooldown 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

  3. Fence(poj1821)

    Fence Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 4705   Accepted: 1489 Description ...

  4. CycleGAN

    目录 概 主要内容 代码 Zhu J., Park T., Isola P. & Efros A. Unpaired Image-to-Image Translation using Cycl ...

  5. Conditional Generative Adversarial Nets

    目录 引 主要内容 代码 Mirza M, Osindero S. Conditional Generative Adversarial Nets.[J]. arXiv: Learning, 2014 ...

  6. Java Web程序设计笔记 • 【第6章 Servlet技术进阶】

    全部章节   >>>> 本章目录 6.1 应用 Servlet API(一) 6.1.1 Servlet 类的层次结构 6.1.2 使用 Servlet API 的原则 6.1 ...

  7. 【MySQL作业】连接查询综合应用——美和易思连接查询综合应用习题

    点击打开所使用到的数据库>>> 1.统计每件商品的销售数量和销售金额,要求按照销售量和销售金额升序显示商品名.销售量和销售金额, 由于需要统计每件商品的销售数量和销售金额,即便某种商 ...

  8. 《MySQL数据操作与查询》- 综合项目 - 航空售票系统

    Mysql & SqlServer综合项目需求 1.系统整体功能 系统需支持以下功能: 维护客户信息.航班信息和票务信息 支持客户按多种条件组合查询航班信息和票务信息 支持客户根据票务信息订购 ...

  9. 编写Java程序,实现简单的五子棋博弈游戏(美和易思Java练习习题)

    package com.qq.gb; import java.util.Scanner; public class GoBang { Scanner sc = new Scanner(System.i ...

  10. SpringBoot集成MyBatis-Plus框架详细方法

    1.说明 本文详细介绍Spring Boot集成MyBatis-Plus框架的方法, 使用MySQL数据库进行测试, 包括完整的开发到测试步骤, 从一开始的Spring Boot工程创建, 到MySQ ...