class db(object): """docstring for db""" _config = conn.connStr() _cnx = None _cur = None def __init__(self): try: self._cnx = mysql.connector.connect(**self._config) self._cur = self._cnx.cursor() except mysql.connector.Error as err: if err.errno == errorcode.ER_ACCESS_DENIED_ERROR: print("Something is wrong with your user name or password") elif err.errno == errorcode.ER_BAD_DB_ERROR: print("Database does not exist") else: print(err) self._cnx.close() def query(self, query, params=None): return self._cur.execute(query, params) def __del__(self): self._cnx.close()
def get_connection(): """ returns a connection to the database """ global _connection global _connector _connector = mysql.connector try: if not _connection: _connection = _connector.connect(**conn.connStr()) except mysql.connector.Error as err: if err.errno == errorcode.ER_ACCESS_DENIED_ERROR: print("Something is wrong with your user name or password") elif err.errno == errorcode.ER_BAD_DB_ERROR: print("Database does not exist") else: print(err) return _connection
#!/usr/bin/python This script provides all back-end functionality to the Digital Recipe Library """ import os import cgi import cgitb import json import mysql.connector from mysql.connector import errorcode import sys sys.path.append('./') import conn cgitb.enable() config = conn.connStr() def cgiFieldStorageToDict(fieldStorage): # get a plain dictionary, rather that the '.value' system used by the # cgi module params = {} for key in fieldStorage.keys(): params[key] = fieldStorage[key].value return params def createItem(info): # create items into the database # dbg statement #sys.stderr.write("info: %s" % info['entity'])