diff options
Diffstat (limited to 'src/jfrteamy')
-rw-r--r-- | src/jfrteamy/__init__.py | 0 | ||||
-rw-r--r-- | src/jfrteamy/db.py | 47 |
2 files changed, 47 insertions, 0 deletions
diff --git a/src/jfrteamy/__init__.py b/src/jfrteamy/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/jfrteamy/__init__.py diff --git a/src/jfrteamy/db.py b/src/jfrteamy/db.py new file mode 100644 index 0000000..b66f8d0 --- /dev/null +++ b/src/jfrteamy/db.py @@ -0,0 +1,47 @@ +import sys + + +class TeamyDB(object): + + db_cursor = None + + def __init__(self, settings): + reload(sys) + sys.setdefaultencoding("latin1") + import mysql.connector + self.database = mysql.connector.connect( + user=settings['user'], + password=settings['password'], + host=settings['host'], + port=settings.get('port', 3306), + database=settings['database']) + self.db_cursor = self.database.cursor(buffered=True) + self.db_name = settings['database'] + + def get_cursor(self): + return self.db_cursor + + def __execute_query(self, sql, params): + #print('DEBUG: query (%s): %s | %s' % (self.db_name, sql.replace('\n', ' '), params)) + self.db_cursor.execute(sql, params) + + def fetch(self, sql, params=None): + import mysql.connector + try: + self.__execute_query(sql, params) + row = self.db_cursor.fetchone() + return row + except mysql.connector.Error as e: + print('ERROR: ' + str(e)) + raise IOError(e.errno, str(e), sql) + + def fetch_all(self, sql, params=None): + import mysql.connector + try: + self.__execute_query(sql, params) + results = self.db_cursor.fetchall() + return results + except mysql.connector.Error as e: + print('ERROR: ' + str(e)) + raise IOError( + e.errno, str(e), sql) |