OrientDB连接Python的过程与问题

OrientDB连接Python的过程与问题

下载安装

进入官网:OrientDB社区版下载,下载所需要的版本。
安装之前需要安装Java。

Python连接

注意:现阶段Python不支持OrientDB 3.X 版本,只支持OrientDB 2.X 版本。
如果你使用OrientDB 3.X 版本,会报以下错误:
PyOrientWrongProtocolVersionException:
Protocol version 37 is not supported yet by this client.

Pyorient:使用技巧:Pyorient使用方法

一、你仍然可能无法连接,如果报错:Server sent empty string。这时需要通过OGM来连接。

from pyorient.ogm import Graph, Config

config = Config.from_url('/localhost/mydb','root','root')
g = Graph(config, 'admin', 'admin')

g.client.command('') # 里面写命令即可

通过这种方式可以实现Python编辑OrientDB。
OGM封装了方法,有足够时间的话,可以看下Graph里面的源码,就知道为什么它可以成功连接。

二、查看Graph源码之后我们可以精简代码:

config = Config.from_url('/localhost/mydb', 'root', 'root')

client = pyorient.OrientDB(config.host, config.port, config.serialization_type)
client.connect(config.user, config.cred)
client.db_open('mydb', 'root', 'root')

client.command('') # 里面写命令即可

三、按照此思路可以接着将Config精简,以下是代码。

class OrientDB(object):
	def __init__(self, host="localhost", db_name=None, port=2424, user="", pswd=""):
		self.config = Config(host, port, user, pswd, db_name)
		self.__init_client()
	
	def __init_client(self):
		self.client = pyorient.OrientDB(self.config.host, self.config.port, self.config.serialization_type)
		self.client.connect(self.config.user, self.config.pswd)
		if not self.client.db_exists(self.config.db_name):
			self.client.db_create(self.config.db_name)
		self.client.db_open(self.config.db_name, self.config.user, self.config.cred)

# 用的时候可以这样调用
mydb = OrientDB(host, db, port, user, pswd)
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值