package com.handset.serialportsensor.db;

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

import org.kymjs.kjframe.KJDB;

/**
 * Created by zy on 2019/3/7.
 */


public class DBUpdateHelperMY implements KJDB.DbUpdateListener {
    private static final String TAG = com.suntiago.dblib.DBUpdateHelper.class.getSimpleName();
    public static final int DATABASE_VERSION = 2;

    public DBUpdateHelperMY() {
    }

    public void onCreate(Context context, SQLiteDatabase sqLiteDatabase) {
    }

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if (oldVersion < 2) {
            if (tabbleIsExist(db, "sensor")) {
                db.execSQL("ALTER TABLE media ADD COLUMN tv_tupe_use_times String DEFAULT \'\';");
            }
        }
    }


    public boolean tabbleIsExist(SQLiteDatabase db, String tableName) {
        boolean result = false;
        if (tableName == null) {
            return false;
        }
        Cursor cursor = null;
        try {
            //这里表名可以是Sqlite_master
            String sql = "select count(*) as c from htjc_sensor_history where type ='table' and name ='" + tableName + "'";
            cursor = db.rawQuery(sql, null);
            if (cursor.moveToNext()) {
                int count = cursor.getInt(0);
                if (count > 0) {
                    result = true;
                }
            }

        } catch (Exception e) {
            // TODO: handle exception
        }
        return result;
    }
}
