二月二日

管理数据库中的信息 进行增删改查

package com.zhen.accountbook.db;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

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<>();
String sql = "select * from typetb where kind=" + kind;
Cursor cursor = db.rawQuery(sql, null);
while (cursor.moveToNext()) {
String typename = cursor.getString(cursor.getColumnIndexOrThrow("typename"));
int imageId = cursor.getInt(cursor.getColumnIndexOrThrow("imageId"));
int sImageId = cursor.getInt(cursor.getColumnIndexOrThrow("sImageId"));
int kind1 = cursor.getInt(cursor.getColumnIndexOrThrow("kind"));
int id = cursor.getInt(cursor.getColumnIndexOrThrow("id"));
TypeBean bean = new TypeBean(id, typename, imageId, sImageId, kind1);
list.add(bean);
}
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("account", null, values);
Log.i("TAG", "插入成功");
}

//获取记账表当中某一天的所有数据
public static List<AccountBean> getAccountListOneDayFromAccounttb(int year, int month, int day) {
List<AccountBean> list = new ArrayList<>();
String sql = "select * from account where year=? and month=? and day=? order by id desc";
Cursor cursor = db.rawQuery(sql, new String[]{year + "", month + "", day + ""});
while (cursor.moveToNext()) {
int id = cursor.getInt(cursor.getColumnIndexOrThrow("id"));
String typename = cursor.getString(cursor.getColumnIndexOrThrow("typename"));
int sImageId = cursor.getInt(cursor.getColumnIndexOrThrow("sImageId"));
String beizhu = cursor.getString(cursor.getColumnIndexOrThrow("beizhu"));
String time = cursor.getString(cursor.getColumnIndexOrThrow("time"));
int kind = cursor.getInt(cursor.getColumnIndexOrThrow("kind"));
float money = cursor.getFloat(cursor.getColumnIndexOrThrow("money"));
AccountBean bean = new AccountBean(id, typename, sImageId, beizhu, money, time, year, month, day, kind);
list.add(bean);
}
return list;
}

public static float getMoneyOneMonth(int year, int month, int kind) {
float total = 0.0f;
String sql = "select sum(money) from account where year=? and month=? and kind=?";
Cursor cursor = db.rawQuery(sql, new String[]{year + "", month + "", kind + ""});
if (cursor.moveToFirst()) {
total = cursor.getFloat(cursor.getColumnIndexOrThrow("sum(money)"));
}
return total;
}

public static float getMoneyOneDay(int year, int month, int day, int kind) {
float total = 0.0f;
String sql = "select sum(money) from account where year=? and month=? and day=? and kind=?";
Cursor cursor = db.rawQuery(sql, new String[]{year + "", month + "", day + "", kind + ""});
if (cursor.moveToFirst()) {
total = cursor.getFloat(cursor.getColumnIndexOrThrow("sum(money)"));
}
return total;
}
}
posted @   财神给你送元宝  阅读(14)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示