Menu ▾ ▴

iBoxDB Python Embedded NoSQL Database

# iBoxDB Python Embedded NoSQL Database example 
# ver 1.0

# Download source code to compile 
# if running on win-x64 or linux-x64, download the compiled file.
# https://sourceforge.net/projects/benchmarksql3/files/

# import classes
from _load_iboxdb_aot import *

# set the database's files directory
db_root("../DBRoot")

# set the database's address, a Number.
address = 11

# if in Debug, delete the old files.
#db_debug_deletefile(address)

# new database
pybox = PyBox(address)

# a prototype object to define what types of the fields
proto = { "id": int(0), "value": str("_"), "ver": int(0), "other_fields": "..." }


# setup database
cfg = pybox.get_config()
# setup table
cfg.ensure_table(proto, "table", "id")
# setup index, [optional].
isUnique = False
cfg.ensure_index(proto, "table", isUnique, "value(32)")
# setup a version field, [optional].
cfg.ensure_increment(proto, "table", "ver")
# setup cache, [optional].
cfg.cache_length(1024 * 1024 * 512)

# open the database to use
pybox.open()


# copy field from prototype
obj = copy(proto)
# modify field
id = pybox.new_id()
obj["id"] = id
obj["value"] = 'v_' + str(id)
# insert object to table
pybox.insert("table", obj)
obj = None

# get object from table
obj = pybox.get("table", id)
# print the object
print(obj)
# modify the object
obj["value"] += "++";
pybox.update("table", obj)
obj = None

# use sql-like to query objects
objs = pybox.select("from table where id==?", id)
obj = objs[0]
print(obj)

# delete the object
pybox.delete("table", obj["id"])

# count the table
size = pybox.count("from table where id>?", 0)
print( f"{size}" )



obj = copy(proto)
# modify field
id = pybox.new_id()
obj["id"] = id
obj["value"] = 'v_' + str(id)
# a Array field
obj["items"] = [{ "name": "ABC" }]
# replace =  insert or update
pybox.replace("table", obj)
obj = None
lastId = 0
for x in pybox.select("from table order by id"):
    print(x)
    lastId = x["id"]

print("Add More to : " , lastId)

# add one more item
obj = pybox.get("table", lastId)
obj["items"].append({ "name": "DEF" })
pybox.replace("table", obj)

for x in pybox.select("from table order by id"):
    print(x)

# close database
pybox.close()

print("End.")
Posted by iBoxDB 1 day ago
Attachments:
t2.py (2293 bytes)

Log in to post a comment.