1. 程式人生 > >couchdb安裝及python使用

couchdb安裝及python使用

python支援

下載安裝couchdb-python ,python-httplib2,python-json模組

  貼程式碼

  Post.py

 1 #!/usr/bin/python2.5
 2 #-*- coding:gb2312 -*-
 3 from couchdb.schema import *
 4
 5 class Post(Document):
 6 author = TextField()
 7 subject = TextField()
 8 content = TextField()
 9 tags = ListField(TextField())
 10 comments = ListField(DictField(Schema.build(
 11 comment_author = TextField(),
 12 comment = TextField(),
 13 comment_data = DateTimeField()

 14 )))
 15 date = DateTimeField()

  Test.py

  1 #!/usr/bin/python2.5
  2 #-*- coding:gb2312 -*-
  3 from Post import Post
  4 from couchdb import Server
  5 from datetime import datetime
  6 import binascii
  7
  8 s = Server('http://127.0.0.1:5984')
  9 s.create("blog")
 10 blog = s["blog"]
 11 p = Post(author="Me",subject="Whatever for the subject",
 12 content="Any content",date=datetime.now(),tags=["Python","Couchdb","Blog"])
 13 p.store(blog)
 14 f = open("post1.py","rb")
 15 foo = binascii.b2a_base64(f.read())
 16 blog = s["blog"]
 17 adoc = blog[p.id]
 18 blog.put_attachment(adoc,"post1.py",foo,"text/python")