diff options
author | Michał Klichowicz <emkael@tlen.pl> | 2023-09-30 12:40:40 +0200 |
---|---|---|
committer | Michał Klichowicz <emkael@tlen.pl> | 2023-09-30 12:40:40 +0200 |
commit | 518bdba985a913044e84e82713a8e76f5ddd3301 (patch) | |
tree | 6757ec4d785c07e2c3f34e70b5bd7326fe7c7a2c /src/jfrteamy/db.py |
Initial import script
Diffstat (limited to 'src/jfrteamy/db.py')
-rw-r--r-- | src/jfrteamy/db.py | 47 |
1 files changed, 47 insertions, 0 deletions
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) |