From 95ef1b08ae73b028df0e432bfb67d59ececd3950 Mon Sep 17 00:00:00 2001 From: gau1991 Date: Mon, 22 Dec 2014 14:18:22 +0530 Subject: [PATCH] Added EE MySQL code --- ee/core/mysql.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 ee/core/mysql.py diff --git a/ee/core/mysql.py b/ee/core/mysql.py new file mode 100644 index 00000000..88598ce6 --- /dev/null +++ b/ee/core/mysql.py @@ -0,0 +1,40 @@ +"""EasyEngine MySQL core classes.""" +import pymysql +import configparser +from os.path import expanduser + + +class EEMysql(): + """Method for MySQL connection""" + + def __init__(self): + 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: + self.conn = pymysql.connect(host=host, port=int(port), + user=user, passwd=passwd) + self.cur = self.conn.cursor() + except Exception as e: + print("Unable to connect to database") + return False + + def execute(self, statement): + try: + self.cur.execute(statement) + return True + except Exception as e: + print("Error occured while executing "+statement) + return False