دیتابیس سینگل تون - 2
شنبه, ۱۱ مرداد ۱۳۹۹، ۰۵:۰۰ ب.ظ
کلاس سینگل تون DBHelper که توش از کلاس SQLiteOpenHelper نیو میکنیم.
public class DBHelper {
    private static DBHelper dBHelperInstance;
    private CreateTable createTable;
    private SQLiteDatabase database;
    private int count = 0;
    public static synchronized void init(Context context) {
        if (dBHelperInstance == null) {
            dBHelperInstance = new DBHelper(context);
        }
    }
    private DBHelper(Context context) {
        createTable = new CreateTable(context);
    }
    public static synchronized DBHelper getInstance() {
        if (dBHelperInstance == null) {
            throw new IllegalStateException(DBHelper.class.getSimpleName() +
                    " is not initialized, call init(..) method first.");
        }
        return dBHelperInstance;
    }
    public synchronized void openDatabase() {
        count++;
        if (count == 1) {
            database = createTable.getWritableDatabase();
        }
    }
    public synchronized void closeDatabese() {
        count--;
        if (count == 0) {
            createTable.close();
        }
    }
متد insert
متد select
متد update
متد delete
۹۹/۰۵/۱۱



