How to find out if a class exists on an OrientDB using PyOrient?
Question: How is it possible to find out if a class exists; this allowing the prevention
of a 'class x already exists in current database' error message?

I have seen the following
[Question](https://stackoverflow.com/questions/28288268/check-class-creation-
in-orientdb), which gives answers in Java and SQL. I'm looking for the Python
equivalent.


Answer: I created the following example in `pyorient`:

**MY STRUCTURE:**

[![enter image description
here](http://i.stack.imgur.com/M72fL.png)](http://i.stack.imgur.com/M72fL.png)

**PyORIENT CODE:**

    
    
    import pyorient
    
    db_name = 'Stack37277880'
    
    print("Connecting to the server...")
    client = pyorient.OrientDB("localhost",2424)
    session_id = client.connect("root","root")
    print("OK - sessionID: ",session_id,"\n")
    
    if client.db_exists( db_name, pyorient.STORAGE_TYPE_PLOCAL ):
        client.db_open(db_name, "root", "root")
        dbClasses = client.command("SELECT name FROM (SELECT expand(classes) FROM metadata:schema)")
        newClass = "MyClass"
        classFound = False
        for idx, val in enumerate(dbClasses):
            if (val.name == newClass):
                classFound = True
                break
        if (classFound != True):
            client.command("CREATE CLASS " + newClass)
            print("Class " + newClass + " correctly created")
        else:
            print("Class " + newClass + " already exists into the DB")
    
    client.db_close() 
    

**First Run Output:**

    
    
    Connecting to the server...
    OK - sessionID:  70 
    
    Class MyClass correctly created
    

**OrientDB Studio:**

[![enter image description
here](http://i.stack.imgur.com/lRRRB.png)](http://i.stack.imgur.com/lRRRB.png)

**Second Run Output:**

    
    
    Connecting to the server...
    OK - sessionID:  74 
    
    Class MyClass already exists into the DB
    

Hope it helps

