个人冲刺(三)——家庭记账本

冲刺任务:完成DataBaseHelper.java

DataBaseHelper.java

package com.example.jizhangben;

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

/**
 * Created by 惠普 on 2021/2/20.
 */

public class DatabaseHelper extends SQLiteOpenHelper {

    public static final String COST_MONEY = "cost_money";
    public static final String COST_DATE = "cost_date";
    public static final String COST_TITLE = "cost_title";
    public static final String IMOOC_COST = "Homework";

    public DatabaseHelper(Context context) {
        super(context, "homework", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table if not exists Homework(" +
                "id integer primary key, " +
                "cost_title varchar, "+
                "cost_date varchar, "+
                "cost_money varchar)");
    }

    public void insertCost(CostBean costBean){
        SQLiteDatabase database=getWritableDatabase();
        ContentValues cv=new ContentValues();
        cv.put(COST_TITLE,costBean.CostTitle);
        cv.put(COST_DATE,costBean.CostDate);
        cv.put(COST_MONEY,costBean.CostMoney);
        database.insert(IMOOC_COST,null,cv);
    }

    public Cursor getAllCostData(){
        SQLiteDatabase database=getWritableDatabase();
        return database.query(IMOOC_COST, null, null, null, null, null, COST_DATE+" ASC");
    }

    public void deleteAllData(){
        SQLiteDatabase database=getWritableDatabase();
        database.delete(IMOOC_COST,null,null);
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

 

posted @ 2021-02-28 20:44  CherriesOvO  阅读(45)  评论(0编辑  收藏  举报