HOME/Articles/

mysql example lern DBcm (snippet)

Article Outline

Python mysql example 'lern DBcm'

Modules used in program:

  • import mysql.connector

python lern DBcm

Python mysql example: lern DBcm

import mysql.connector


class UseDatabase:

    def __init__(self, config: dict) -> None:
        self.configuration = config

    def __enter__(self) -> 'cursor':
        self.conn = mysql.connector.connect(**self.configuration)
        self.cursor = self.conn.cursor()
        return self.cursor

    def __exit__(self, exc_type, exc_value, exc_trace):
        self.conn.commit()
        self.cursor.close()
        self.conn.close()