def connect(): """ Connect to the PostgreSQL database server """ conn = None try: # read connection parameters params = config() # connect to the PostgreSQL server print('Connecting to the PostgreSQL database...') conn = psycopg2.connect(**params) # create a cursor cur = conn.cursor() # execute a statement print('PostgreSQL database version:') cur.execute('SELECT version()') # display the PostgreSQL database server version db_version = cur.fetchone() print(db_version) # close the communication with the PostgreSQL cur.close() return conn except (Exception, psycopg2.DatabaseError) as error: print(error)
def initMongoDBConn(component=None): conf = config(section='mongodb') try: client = MongoClient('{host}:{port}'.format(host=conf['host'], port=conf['port'])) mongodb_conn = client[component] except ValueError as e: print('MongoDB connection to {host} is refused'.format( host=conf['host'])) return mongodb_conn
def create_result_row(conn, result): sql = ''' INSERT INTO acm(year, location, place, squad, coach) VALUES (%s,%s,%s,%s,%s) ''' conn = None try: # read database configuration params = config() conn = pymysql.connect(**params) # create a new cursor cur = conn.cursor() # execute the INSERT statement if len(result) != 5: result.append('') cur.execute(sql, result) conn.commit() except (Exception, pymysql.DatabaseError) as error: print(error) return cur.lastrowid