2016-05-17 73 views

回答

0

我创建在pyorient以下示例:

MY结构:

enter image description here

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() 

首次运行输出:

Connecting to the server... 
OK - sessionID: 70 

Class MyClass correctly created 

OrientDB演播室:

enter image description here

第二次运行输出:

Connecting to the server... 
OK - sessionID: 74 

Class MyClass already exists into the DB 

希望它可以帮助

+0

感谢@LucaS你的时间,这是一个很大的帮助! – geminiCoder

+0

如果PyOrient将这个功能作为一个单一功能,那将会是一件很酷的事情。我会看看源代码的目的是为了添加它。 – geminiCoder

+0

Hi @geminiCoder,感谢您的投票。很高兴有帮助。 – LucaS

-1

您可以使用与Java示例中相同的查询。

import pyorient 

className = "MyClass" 

database = pyorient.OrientDB("localhost", 2424) 
database.db_open(
"DB_name", 
"user", 
"password" 
) 

if not database.command("SELECT FROM (SELECT expand(classes) FROM metadata:schema) WHERE name = '%s'" % className): 
    print("Create class %s" % className) 
    database.command("CREATE CLASS %s EXTENDS V" % className) 
else: 
    print("Class already exist.") 
相关问题