package com.example.myapplication.repository;

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

public final class DatabaseManager {
    private static DatabaseHelper helper;
    private static SQLiteDatabase db;

    private DatabaseManager() {}

    public static synchronized void init(Context ctx) {
        if (helper == null) {
            helper = new DatabaseHelper(ctx.getApplicationContext());
            db = helper.getWritableDatabase(); // 打开并保持
        }
    }

    public static synchronized SQLiteDatabase getDb() {
        if (db == null && helper != null) {
            db = helper.getWritableDatabase();
        }
        return db;
    }

    public static synchronized void close() {
        if (helper != null) {
            helper.close();
            db = null;
            helper = null;
        }
    }
}