python保存数据db_用python操作和管理ArangoDB

目录:

安装需要用到的python包:

pip install pyarango

一、连接数据库:

>>> from pyArango.connection import *

>>> conn = Connection(username="root", password="root_passwd")

当该代码执行时,它会初始化 conn 变量上的服务器连接。默认情况下,pyArango会尝试建立与http://127.0.0.1:8529的连接。

二、创建数据库/集合/文档

创建和打开数据库

方法:

createDatabase()

该方法可以在服务器上打开或创建数据库,当要连接的数据库不存在时,pyArango会在服务器上创建它。当它存在时,pyArango会尝试打开数据库。

>>> db = conn.createDatabase(name="school")

也可以使用其名称作为服务器连接上的键来打开现有数据库:

>>> db = conn["school"]>>>db

ArangoDB database: school

创建集合

方法:

createCollection()

>>> studentsCollection = db.createCollection(name="Students")>>> db["Students"]

ArangoDB Collection name: Students,id: 202, type: document, status loaded

创建文档

方法:

createDocument()

>>> doc1 =studentsCollection.createDocument()>>> doc1["name"] = "John Smith"

>>>doc1

ArangoDoc‘None‘: {‘name‘: ‘John Smith‘}>>> doc2 =studentsCollection.createDocument()>>> doc2["firstname"] = "Emily"

>>> doc2["lastname"] = "Bronte"

>>>doc2

ArangoDoc‘None‘: {‘firstname‘: ‘Emily‘, ‘lastname‘: ‘Bronte‘}

因为尚未将其保存到ArangoDB,所以该文档显示其 _id 为“None”。这意味着该变量存在于您的Python代码中,但不存在于数据库中。 ArangoDB 通过将集合名称与 __key 值进行配对来构造 _id 值。

保存文档:

>>> doc1._key = "johnsmith"

>>>doc1.save()>>>doc1

ArangoDoc‘Students/johnsmith‘: {‘name‘: ‘John Smith‘}

循环输入数据:

>>> students = [(‘Oscar‘, ‘Wilde‘, 3.5), (‘Thomas‘, ‘Hobbes‘, 3.2),

... (‘Mark‘, ‘Twain‘, 3.0), (‘Kate‘, ‘Chopin‘, 3.8), (‘Fyodor‘, ‘Dostoevsky‘, 3.1),

... (‘Jane‘, ‘Austen‘,3.4), (‘Mary‘, ‘Wollstonecraft‘, 3.7), (‘Percy‘, ‘Shelley‘, 3.5),

... (‘William‘, ‘Faulkner‘, 3.8), (‘Charlotte‘, ‘Bronte‘, 3.0)]>>> for (first, last, gpa) instudents:

... doc=studentsCollection.createDocument()

... doc[‘name‘] = "%s %s" % (first, last)

... doc[‘gpa‘] =gpa

... doc[‘year‘] = 2017... doc._key= ‘‘.join([first, last]).lower()

... doc.save()

三、检索筛选

查看某一个特定学生的GPA:

>>>def report_gpa(document):

... print("Student: %s" % document[‘name‘])

... print("GPA: %s" % document[‘gpa‘])>>> kate = studentsCollection[‘katechopin‘]>>>report_gpa(kate)

Student: Kate Chopin

GPA:3.8

筛选平均成绩在3.5以上的学生:

方法:

fetchAll()

>>>def top_scores(col, gpa):

... print("Top Soring Students:")

...for student incol.fetchAll():

...if student[‘gpa‘] >=gpa:

... print("- %s" % student[‘name‘])>>> top_scores(studentsCollection, 3.5)

Top Scoring Students:-Mary Wollstonecraft-Kate Chopin-Percy Shelly-William Faulkner- Oscar Wilde

四、更新

可以定义一个特定的函数来处理更新:

>>>def update_gpa(key, new_gpa):

... doc=studentsCollection[key]

... doc[‘gpa‘] =new_gpa

... doc.save()

五、删除

方法:

delete()

>>> tom = studentsCollection["thomashobbes"]>>>tom.delete()>>> studentsCollection["thomashobbes"]

KeyError: (‘Unable to find document with _key: thomashobbes‘, {‘code‘: 404,‘errorNum‘: 1202,‘errorMessage‘: ‘document Students/thomashobbes not found‘,‘error‘: True

})

六、调用AQL的方法

除了上面显示的Python方法之外,ArangoDB还提供了一种查询语言(称为AQL),用于检索和修改数据库上的文档。在pyArango中,您可以使用 AQLQuery() 方法执行这些查询。

检索所有文档的_key:

>>> aql = "FOR x IN Students RETURN x._key"

>>> queryResult = db.AQLQuery(aql, rawResults=True, batchSize=100)>>> for key inqueryResult:

... print(key)

marywollstonecraft

katechopin

percyshelley

fyodordostoevsky

marktwain

...

参考资料:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值