def create_connection_mysql(): """ create a connection to the mysql database specified by host, user, password and database name defined in config.ini :param :return: Connection object """ mydb = create_default_connection_mysql() mydb.database = cfg.getInstance().database return mydb
def create_default_connection_mysql(): """ Create a default connection to the mysql database specified by host, user and password defined in config.ini :param :return mydb: connection object """ conf = cfg.getInstance() mydb = mysql.connector.connect(host=conf.host, user=conf.user, password=conf.password) return mydb
def create_engine_mysql(): """ Create a pool and dialect together connection to provide a source of database and behavior :param: :return: Connection engine """ # in case of connection error, change the host as in the next commented code conf = cfg.getInstance() host = conf.host user = conf.user passwd = conf.password db_name = conf.database return create_engine("mysql+pymysql://{user}:{pw}@{host}/{db}".format( user=user, pw=passwd, host=host, db=db_name), pool_pre_ping=True)
def check_database(): """ check the existence of a database with the name defined in config.ini if none, a new is created as well as a table of similarity :param: none :return: """ global mydb, mycursor try: check = False mydb = create_default_connection_mysql() mycursor = mydb.cursor() db_name = cfg.getInstance().database mycursor.execute("SHOW DATABASES") for x in mycursor: # x = x[0].decode("unicode-escape") # decode was giving an error (note: decode is needed when using mysql docker) if x[0].decode("unicode-escape") == db_name: # x[0].encode().decode('utf-8') == db_name: check = True if check != False: print("Database already exists") else: print( "The database is not available. Calculate similarity based " + "on SemanticSimDBCreator project") except Error as e: print("Error while connecting to MySQL", e) finally: if mydb.is_connected(): mycursor.close() mydb.close()
def check_database(): """ Check the existence of a database with the name defined in config.ini if none, a new is created as well as a table of similarity :param :return none """ global mydb try: check = False mydb = create_default_connection_mysql() mycursor = mydb.cursor() db_name = cfg.getInstance().database mycursor.execute("SHOW DATABASES") for x in mycursor: if x[0].decode("unicode-escape") == db_name: # or, # if x[0].encode().decode( 'utf-8' ) == db_name: check = True if not check: print("Will create database") mycursor.execute("CREATE DATABASE " + db_name) create_table() else: print("Database already exists") except Error as e: print("Error while connecting to MySQL", e) finally: if mydb.is_connected(): mycursor.close() mydb.close()
pd.set_option('display.max_columns', None) if __name__ == '__main__': start_time = datetime.now() # ---------------------------------------------------------------------------------------- # # connect db check_database() # ---------------------------------------------------------------------------------------- # # read dataset; select the right columns; select items only from ontology # you must df_dataset = upload_dataset( cfg.getInstance().dataset,\ cfg.getInstance().item_prefix ) # ---------------------------------------------------------------------------------------- # # check ontology database if os.path.isfile(cfg.getInstance().path_to_ontology): print("Database ontology file already exists") else: print("Database from owl does not exit. Creating...") ssmpy.create_semantic_base( cfg.getInstance().path_to_owl, cfg.getInstance().path_to_ontology, "http://purl.obolibrary.org/obo/", "http://www.w3.org/2000/01/rdf-schema#subClassOf", "")