From 518bdba985a913044e84e82713a8e76f5ddd3301 Mon Sep 17 00:00:00 2001 From: MichaƂ Klichowicz Date: Sat, 30 Sep 2023 12:40:40 +0200 Subject: Initial import script --- src/jfrteamy/__init__.py | 0 src/jfrteamy/db.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 src/jfrteamy/__init__.py create mode 100644 src/jfrteamy/db.py (limited to 'src/jfrteamy') diff --git a/src/jfrteamy/__init__.py b/src/jfrteamy/__init__.py new file mode 100644 index 0000000..e69de29 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) -- cgit v1.2.3