def database_connect(): """ Connects to the database using the connection string. If 'None' was returned it means there was an issue connecting to the database. It would be wise to handle this ;) """ # Read the config file config = configparser.ConfigParser() config.read('config.ini') if 'database' not in config['DATABASE']: config['DATABASE']['database'] = config['DATABASE']['user'] # Create a connection to the database connection = None try: # Parses the config file and connects using the connect string connection = pg8000.connect(database=config['DATABASE']['database'], user=config['DATABASE']['user'], password=config['DATABASE']['password'], host=config['DATABASE']['host']) except pg8000.OperationalError as operation_error: print("""Error, you haven't updated your config.ini or you have a bad connection, please try again. (Update your files first, then check internet connection) """) print(operation_error) return None # return the connection to use return connection
def database_connect(): # Read the config file config = configparser.ConfigParser() config.read('config.ini') # Create a connection to the database connection = None try: connection = pg8000.connect(database=config['DATABASE']['user'], user=config['DATABASE']['user'], password=config['DATABASE']['password'], host=config['DATABASE']['host']) except pg8000.OperationalError as e: print("""Error, you haven't updated your config.ini or you have a bad connection, please try again. (Update your files first, then check internet connection) """) print(e) #return the connection to use return connection