Python 3.x 如何使用PyOrient确定OrientDB上是否存在类?
如何才能发现类是否存在;这是否允许防止“当前数据库中已存在x类”错误消息Python 3.x 如何使用PyOrient确定OrientDB上是否存在类?,python-3.x,orientdb,pyorient,Python 3.x,Orientdb,Pyorient,如何才能发现类是否存在;这是否允许防止“当前数据库中已存在x类”错误消息 我已经看到了以下内容,它们用Java和SQL给出了答案。我正在寻找与Python等效的版本。我在pyorient中创建了以下示例: 我的结构: import pyorient db_name = 'Stack37277880' print("Connecting to the server...") client = pyorient.OrientDB("localhost",2424) session_id = cl
我已经看到了以下内容,它们用Java和SQL给出了答案。我正在寻找与Python等效的版本。我在
pyorient
中创建了以下示例:
我的结构:
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
Connecting to the server...
OK - sessionID: 74
Class MyClass already exists into the DB
PyORIENT代码:
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
Connecting to the server...
OK - sessionID: 74
Class MyClass already exists into the DB
首次运行输出:
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
Connecting to the server...
OK - sessionID: 74
Class MyClass already exists into the DB
OrientDB Studio:
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
Connecting to the server...
OK - sessionID: 74
Class MyClass already exists into the DB
第二次运行输出:
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
Connecting to the server...
OK - sessionID: 74
Class MyClass already exists into the DB
希望对您有所帮助您可以使用与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.")
如果这个功能在PyOrient中作为一个单独的功能出现,那就更酷了。我将看一看源代码,目的是将其添加到中。