# import pyodbc
from urllib.parse import urlparse
import mysql.connector


class DBAccess:
    con = None
    cur = None

    def __init__(self):
        url = urlparse('mysql://typical:Typic%40l3226@217.142.228.191:3306/checklist')

        self.con = mysql.connector.connect(
            host="217.142.228.191",
            port="3306",
            user="typical",
            password="Typic@l3226",
            database="checklist",
        )

        self.cur = self.con.cursor(dictionary=True, buffered=True)

        # try:#
        #     self.con = pyodbc.connect(
        #         "DRIVER={SQL Server};"
        #         "SERVER=171.2.1.140;"
        #         "UID=sa;PWD=typical;"
        #         "DATABASE=M_Checklist"
        #     )
        #     self.cur = self.con.cursor()
        # except (pyodbc.Error) as e:
        #     print(e)

    def __del__(self):
        self.con.close()
        self.cur.close()

    def select_rows(self, str_select):
        self.cur.execute(str_select)
        return self.cur.fetchall()

    def select_row(self, str_select):
        self.cur.execute(str_select)
        return self.cur.fetchone()

    def regist_db(self, str_select):
        self.cur.execute(str_select)

    def commit(self):
        self.con.commit()
