记账app数据库管理代码

package com.hui.tally.db;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

/*
负责管理数据库的类
主要对于表当中的内容进行操作,增删改查

 */
public class DBManger {
    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<>();
        //读取typeb表当中的数据
        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 typeBean = new TypeBean(id,typename,imageId,sImageId,kind);
            list.add(typeBean);

        }
    }


}

 

posted @ 2022-02-25 10:05  高槐玉在隐身  阅读(111)  评论(0)    收藏  举报