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, which gives answers in Java and SQL. I'm looking for the Python equivalent.
I created the following example in pyorient
:
MY STRUCTURE:
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:
Second Run Output:
Connecting to the server...
OK - sessionID: 74
Class MyClass already exists into the DB
Hope it helps