package com.emato.ich.data;

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

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * 实例化后调用getWritableDatabase()就会创建数据库文件
 * 如何查看? 把数据库文件导出来, 使用SQLite工具即可打开, Android SDK安装目录下的platform-tools目录里就有
 */
public class SQLiteOpenHelperImpl extends SQLiteOpenHelper {

    public SQLiteOpenHelperImpl(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, "ich.db", factory, 1);
    }

    public SQLiteOpenHelperImpl(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version, @Nullable DatabaseErrorHandler errorHandler) {
        super(context, "ich.db", factory, 1, errorHandler);
    }

    public SQLiteOpenHelperImpl(@Nullable Context context, @Nullable String name, int version, @NonNull SQLiteDatabase.OpenParams openParams) {
        super(context, "ich.db", 1, openParams);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        // 初始化表
        sqLiteDatabase.execSQL("");
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
        // 数据库版本发生变化时被调用
        sqLiteDatabase.execSQL("ALTER TABLE person ADD phone VARCHAR(12) NOT NULL");
    }
}
