File: kcdbex1.py

package info (click to toggle)
python-kyotocabinet 1.23-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 828 kB
  • sloc: cpp: 2,967; python: 1,241; javascript: 249; makefile: 85
file content (35 lines) | stat: -rw-r--r-- 799 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from kyotocabinet import *
import sys

# create the database object
db = DB()

# open the database
if not db.open("casket.kch", DB.OWRITER | DB.OCREATE):
    print("open error: " + str(db.error()), file=sys.stderr)

# store records
if not db.set("foo", "hop") or \
        not db.set("bar", "step") or \
        not db.set("baz", "jump"):
    print("set error: " + str(db.error()), file=sys.stderr)

# retrieve records
value = db.get_str("foo")
if value:
    print(value)
else:
    print("get error: " + str(db.error()), file=sys.stderr)

# traverse records
cur = db.cursor()
cur.jump()
while True:
    rec = cur.get_str(True)
    if not rec: break
    print(rec[0] + ":" + rec[1])
cur.disable()

# close the database
if not db.close():
    print("close error: " + str(db.error()), file=sys.stderr)