安卓记账本开发——数据库创建和数据测试

chibangyuxun 2020-02-14

项目已同步到 GitHub https://github.com/YourDeepEnd/HouseHoldBook


目前界面

安卓记账本开发——数据库创建和数据测试


部分源码

DatabaseHelper.java
package com.example.householdbook;

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

import androidx.annotation.Nullable;

public class DatabaseHelper extends SQLiteOpenHelper {


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

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

    public void insertCost(CostBean c){

        SQLiteDatabase database = getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put("cost_title",c.costTitle);
        cv.put("cost_date",c.costDate);
        cv.put("cost_money",c.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 deleteAllCostData(){
        SQLiteDatabase database = getWritableDatabase();
         database.delete("imooc_cost",null,null);
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

相关推荐