家庭记账本开发记录4

为了能够储存信息,所以需要了解安卓中的数据库sqlite

Sqlite的创建:

package com.example.mybookkeeping.dao;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import androidx.annotation.Nullable;

public class SQLite_DB extends SQLiteOpenHelper {
    public SQLite_DB(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql = "create table  count(" +
                "type char(5)," +
                "info char(30)," +
                "date char(30)," +
                "money char(20));";
        Log.i("SQLite_DB", "onCreate: DoThis");

        db.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }


}

sqlite的使用:

//有写权限的sqlite类
sqLite_db = new SQLite_DB(IncomeActivity.this, "test1.db", null, 1); wdb = sqLite_db.getWritableDatabase(); contentValues.put("money", budget.getMoney()); contentValues.put("type", budget.getType()); contentValues.put("date", budget.getDate()); contentValues.put("info", budget.getPurposes()); long l = wdb.insert("count", null, contentValues); if (l > 0) { Toast.makeText(IncomeActivity.this, "提交成功", Toast.LENGTH_SHORT).show(); }
//分别创建有读写权限的实例
sqLite_db = new SQLite_DB(MainActivity.this, "test1.db", null, 1); wdb = sqLite_db.getWritableDatabase(); rdb = sqLite_db.getReadableDatabase();
原文地址:https://www.cnblogs.com/MXming/p/14916018.html