You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
2.1 KiB

"""EasyEngine MySQL core classes."""
import pymysql
import configparser
from os.path import expanduser
import sys
from ee.core.logging import Log
class EEMysql():
"""Method for MySQL connection"""
def execute(self, statement, errormsg='', log=True):
"""Get login details from ~/.my.cnf & Execute MySQL query"""
config = configparser.RawConfigParser()
cnfpath = expanduser("~")+"/.my.cnf"
if [cnfpath] == config.read(cnfpath):
user = config.get('client', 'user')
passwd = config.get('client', 'password')
try:
host = config.get('client', 'host')
except configparser.NoOptionError as e:
host = 'localhost'
try:
port = config.get('client', 'port')
except configparser.NoOptionError as e:
port = '3306'
try:
conn = pymysql.connect(host=host, port=int(port),
user=user, passwd=passwd)
cur = conn.cursor()
except Exception as e:
10 years ago
if errormsg:
10 years ago
Log.debug(self, '{0}'
.format(e))
10 years ago
Log.error(self, '{0}'
.format(errormsg))
else:
10 years ago
Log.debug(self, '{0}'
.format(e))
10 years ago
Log.error(self, 'Unable to connect to database: {0}'
.format(e))
try:
if log:
Log.debug(self, "Executing MySQL statement: {0}"
.format(statement))
cur.execute(statement)
cur.close()
conn.close()
except Exception as e:
cur.close()
conn.close()
Log.debug(self, "{0}".format(e))
if not errormsg:
Log.error(self, 'Unable to execute statement')
else:
Log.error(self, '{0}'.format(errormsg))
# def __del__(self):
# self.cur.close()
# self.conn.close()