| 1234567891011121314151617181920212223242526272829303132333435363738394041 |
- """
- Mysql utilities
- @author: olivier.massot, 05-2020
- """
- import mysql
- class MySqlServer:
- def __init__(self, host, port, username, password, description=""):
- self.host = host
- self.port = port
- self.username = username
- self.password = password
- self.description = description or "no description"
- self.cnn = None
- def __repr__(self):
- return f"{self.username}@{self.host}:{self.port} ({self.description})"
- def connect(self):
- self.cnn = mysql.connector.connect(
- host=self.host,
- port=self.port,
- user=self.username,
- passwd=self.password
- )
- def exec(self, sql):
- """ Execute the sql code and return the cursor """
- cursor = self.cnn.cursor()
- logger.debug(sql)
- cursor.execute(sql)
- return cursor
- def db_exists(self, dbname):
- cursor = self.exec(f"""SELECT SCHEMA_NAME
- FROM INFORMATION_SCHEMA.SCHEMATA
- WHERE SCHEMA_NAME = '{dbname}'""")
- row = cursor.fetchone()
- return row is not None
|