<gh_stars>0
import mysql.connector
import json


def connect():
    try:
        mysql_username = Details.get("mysql_username")
        mysql_password = Details.get("mysql_password")
        connector = mysql.connector.connect(host="localhost", user=mysql_username, password=mysql_password)
        Details.enter("setup", True)
        connector.close()
        return True
    except mysql.connector.errors.ProgrammingError:
        Details.enter("setup", False)
        return False


def initialization():
    # connecting to server
    mysql_username = Details.get("mysql_username")
    mysql_password = Details.get("mysql_password")
    db = mysql.connector.connect(host="localhost", user=mysql_username, password=mysql_password)

    # checking for database
    cursor = db.cursor()
    cursor.execute("SHOW DATABASES")
    databases = [database[0] for database in cursor]
    if "school" not in databases:
        cursor.execute("CREATE DATABASE school")

    # connecting to database
    cursor.execute("USE school")

    # checking for table
    cursor = db.cursor()
    cursor.execute("SHOW TABLES")
    tables = [table[0] for table in cursor]

    def initial_insert():
        # inserting records into the table
        records = [
            (42610, "riya", 12, 9717242100,"<EMAIL>"),
            (45672, "kedar", 12, 7865649906, "<EMAIL>"),
            (23489, "hiten", 12, 8118844212, "<EMAIL>"),
            (34578, "gautam", 12, 8301571283, "<EMAIL>"),
            (34890, "anish", 12, 7254004825, "<EMAIL>"),
            (56790, "sriya", 12, 8174617893, "56790<EMAIL>"),
            (45678, "aashritha", 12, 9149742429, "<EMAIL>"),
            (23456, "lasya", 12,  7102225654,"<EMAIL>"),
            (34590, "sohan", 12 , 9601914938, "<EMAIL>"),
            (99856, "shreyas", 12, 8448977437, "<EMAIL>"),
            ]

        for record in records:
            query = f"INSERT INTO students VALUES {record[0], record[1], record[2], record[3], record[4]}"
            cursor.execute(query)

    if "students" in tables:
        cursor.execute("DELETE FROM students")
        initial_insert()
        pass
    elif "students" not in tables:
        # creating table
        cursor.execute('''CREATE TABLE students (
                        admin_number int NOT NULL Primary Key, 
                        name varchar(32) NOT NULL, 
                        class int NOT NULL, 
                        mobile_number varchar(10) NOT NULL, 
                        email varchar(32) NOT NULL)'''
                        )
        initial_insert()

    db.commit()

    # close
    cursor.close()
    db.close()

    Details.enter("initialization", True)


class Database:
    def __init__(self):
        # connecting to database
        username = Details.get("mysql_username")
        password = Details.get("mysql_password")

        self.db = mysql.connector.connect(host="localhost",user=username, password=password, database="school")
        self.cursor = self.db.cursor()

    def display(self):
        self.cursor.execute("Select * FROM students")
        records = [record for record in self.cursor.fetchall()]
        return records

    def insert(self, data):
        values = tuple(data)
        query = """INSERT INTO students (admin_number, name, class, mobile_number, email) VALUES(%s, %s, %s, %s, %s)"""
        self.cursor.execute(query, values)
        self.db.commit()

    def search(self, admin_number):
        self.cursor.execute(f"""SELECT * FROM students WHERE admin_number = \"{admin_number}\"""")
        records = [record for record in self.cursor.fetchall()]
        return records

    def update(self, admin_number, field, value):
        self.cursor.execute(f"""UPDATE students SET {field} = '{value}' WHERE admin_number = '{admin_number}'""")
        self.db.commit()
        return self.cursor.rowcount

    def delete(self, admin_number):
        self.cursor.execute(f"""DELETE FROM students WHERE admin_number = \"{admin_number}\"""")
        self.db.commit()
        return self.cursor.rowcount

    def close(self):
        self.cursor.close()
        self.db.close()


class Auth:
    def __init__(self):
        self.username = Details.get("username")
        self.password = Details.get("password")
        
    def auth(self, username, password):
        if username == self.username:
            if password == self.password:
                return True
            else:
                return False
        else:
            return False

    @staticmethod
    def change(username=None, password=None):
        if username:
            Details.enter("username", username)
        if password:
            Details.enter("password", password)


class Details:
    @staticmethod
    def enter(key, value):
        with open("./data/details.json", "r") as file:
            details = json.load(file)
            details[key] = value
        
        with open("./data/details.json", "w") as file:
            json.dump(details, file, indent=4)
                
    @staticmethod
    def get(key):
        with open("./data/details.json", "r") as file:
            details = json.load(file)
            return details[key]
            

if __name__ == "__main__":
    pass

import mysql.connector
import json


def connect():
    try:
        mysql_username = Details.get("mysql_username")
        mysql_password = Details.get("mysql_password")
        connector = mysql.connector.connect(host="localhost", user=mysql_username, password=mysql_password)
        Details.enter("setup", True)
        connector.close()
        return True
    except mysql.connector.errors.ProgrammingError:
        Details.enter("setup", False)
        return False


def initialization():
    # connecting to server
    mysql_username = Details.get("mysql_username")
    mysql_password = Details.get("mysql_password")
    db = mysql.connector.connect(host="localhost", user=mysql_username, password=mysql_password)

    # checking for database
    cursor = db.cursor()
    cursor.execute("SHOW DATABASES")
    databases = [database[0] for database in cursor]
    if "school" not in databases:
        cursor.execute("CREATE DATABASE school")

    # connecting to database
    cursor.execute("USE school")

    # checking for table
    cursor = db.cursor()
    cursor.execute("SHOW TABLES")
    tables = [table[0] for table in cursor]

    def initial_insert():
        # inserting records into the table
        records = [
            (42610, "riya", 12, 9717242100,"<EMAIL>"),
            (45672, "kedar", 12, 7865649906, "<EMAIL>"),
            (23489, "hiten", 12, 8118844212, "<EMAIL>"),
            (34578, "gautam", 12, 8301571283, "<EMAIL>"),
            (34890, "anish", 12, 7254004825, "<EMAIL>"),
            (56790, "sriya", 12, 8174617893, "56790<EMAIL>"),
            (45678, "aashritha", 12, 9149742429, "<EMAIL>"),
            (23456, "lasya", 12,  7102225654,"<EMAIL>"),
            (34590, "sohan", 12 , 9601914938, "<EMAIL>"),
            (99856, "shreyas", 12, 8448977437, "<EMAIL>"),
            ]

        for record in records:
            query = f"INSERT INTO students VALUES {record[0], record[1], record[2], record[3], record[4]}"
            cursor.execute(query)

    if "students" in tables:
        cursor.execute("DELETE FROM students")
        initial_insert()
        pass
    elif "students" not in tables:
        # creating table
        cursor.execute('''CREATE TABLE students (
                        admin_number int NOT NULL Primary Key, 
                        name varchar(32) NOT NULL, 
                        class int NOT NULL, 
                        mobile_number varchar(10) NOT NULL, 
                        email varchar(32) NOT NULL)'''
                        )
        initial_insert()

    db.commit()

    # close
    cursor.close()
    db.close()

    Details.enter("initialization", True)


class Database:
    def __init__(self):
        # connecting to database
        username = Details.get("mysql_username")
        password = Details.get("mysql_password")

        self.db = mysql.connector.connect(host="localhost",user=username, password=password, database="school")
        self.cursor = self.db.cursor()

    def display(self):
        self.cursor.execute("Select * FROM students")
        records = [record for record in self.cursor.fetchall()]
        return records

    def insert(self, data):
        values = tuple(data)
        query = """INSERT INTO students (admin_number, name, class, mobile_number, email) VALUES(%s, %s, %s, %s, %s)"""
        self.cursor.execute(query, values)
        self.db.commit()

    def search(self, admin_number):
        self.cursor.execute(f"""SELECT * FROM students WHERE admin_number = \"{admin_number}\"""")
        records = [record for record in self.cursor.fetchall()]
        return records

    def update(self, admin_number, field, value):
        self.cursor.execute(f"""UPDATE students SET {field} = '{value}' WHERE admin_number = '{admin_number}'""")
        self.db.commit()
        return self.cursor.rowcount

    def delete(self, admin_number):
        self.cursor.execute(f"""DELETE FROM students WHERE admin_number = \"{admin_number}\"""")
        self.db.commit()
        return self.cursor.rowcount

    def close(self):
        self.cursor.close()
        self.db.close()


class Auth:
    def __init__(self):
        self.username = Details.get("username")
        self.password = Details.get("password")
        
    def auth(self, username, password):
        if username == self.username:
            if password == self.password:
                return True
            else:
                return False
        else:
            return False

    @staticmethod
    def change(username=None, password=None):
        if username:
            Details.enter("username", username)
        if password:
            Details.enter("password", password)


class Details:
    @staticmethod
    def enter(key, value):
        with open("./data/details.json", "r") as file:
            details = json.load(file)
            details[key] = value
        
        with open("./data/details.json", "w") as file:
            json.dump(details, file, indent=4)
                
    @staticmethod
    def get(key):
        with open("./data/details.json", "r") as file:
            details = json.load(file)
            return details[key]
            

if __name__ == "__main__":
    pass
# connecting to server # checking for database # connecting to database # checking for table # inserting records into the table # creating table CREATE TABLE students (                         admin_number int NOT NULL Primary Key,                          name varchar(32) NOT NULL,                          class int NOT NULL,                          mobile_number varchar(10) NOT NULL,                          email varchar(32) NOT NULL) # close # connecting to database INSERT INTO students (admin_number, name, class, mobile_number, email) VALUES(%s, %s, %s, %s, %s) SELECT * FROM students WHERE admin_number = \"{admin_number}\ UPDATE students SET {field} = '{value}' WHERE admin_number = '{admin_number}' DELETE FROM students WHERE admin_number = \"{admin_number}\