import json from rcdb.provider import RCDBProvider from rcdb.model import ConditionType # Create RCDBProvider provider object and connect it to DB db = RCDBProvider("sqlite:///example.db") # Create condition type db.create_condition_type("list_data", ConditionType.JSON_FIELD, "Data list") db.create_condition_type("dict_data", ConditionType.JSON_FIELD, "Data dict") list_to_store = [1, 2, 3] dict_to_store = {"x": 1, "y": 2, "z": 3} # Dump values to JSON and save it to DB to run 1 db.add_condition(1, "list_data", json.dumps(list_to_store)) db.add_condition(1, "dict_data", json.dumps(dict_to_store)) # Get condition from database restored_list = json.loads(db.get_condition(1, "list_data").value) restored_dict = json.loads(db.get_condition(1, "dict_data").value) print restored_list print restored_dict print restored_dict["x"] print restored_dict["y"] print restored_dict["z"]
from datetime import datetime from rcdb.provider import RCDBProvider from rcdb.model import ConditionType # Create RCDBProvider object that connects to DB and provide most of the functions db = RCDBProvider("sqlite:///example.db") # Create condition type db.create_condition_type("my_val", ConditionType.INT_FIELD, description="This is my value") # Add data to database db.add_condition(1, "my_val", 1000) # Replace previous value db.add_condition(1, "my_val", 2000, replace=True) # Add time information to the db.add_condition(1, "my_val", 2000, datetime(2015, 10, 10, 15, 28, 12, 111111), replace=True) # Get condition from database condition = db.get_condition(1, "my_val") print condition print "value =", condition.value print "name =", condition.name print "time =", condition.time_value # Getting type information ct = db.get_condition_type("my_val") print ct
exit(1) if len(sys.argv) > 1: # Open database using connection string from command line argument db = RCDBProvider(sys.argv[1]) else: # Create in-memory database db = RCDBProvider("sqlite://", check_version=False) destroy_all_create_schema(db) # Create run number 1 (in case it doesn't exist) db.create_run(1) # Create condition type db.create_condition_type("cat", ConditionType.JSON_FIELD, "The Cat lives here") # Create a cat and store in in the DB for run 1 cat = Cat('Alice') db.add_condition(1, "cat", jsonpickle.encode(cat)) # Get condition from database for run 1 condition = db.get_condition(1, "cat") loaded_cat = jsonpickle.decode(condition.value) print "How cat is stored in DB:" print condition.value print "Deserialized cat:" print "name:", loaded_cat.name print "mice_eaten:", loaded_cat.mice_eaten
def __init__(self, name): self.name = name self.mice_eaten = 1230 try: import jsonpickle except ImportError: print "no jsonpickle modele installed. It is required for this example" exit(1) # Create RCDBProvider provider object and connect it to DB db = RCDBProvider("sqlite:///example.db") # Create condition type db.create_condition_type("cat", ConditionType.JSON_FIELD, "The Cat lives here") # Create a cat and store in in the DB for run 1 cat = Cat('Alice') db.add_condition(1, "cat", jsonpickle.encode(cat)) # Get condition from database for run 1 condition = db.get_condition(1, "cat") loaded_cat = jsonpickle.decode(condition.value) print "How cat is stored in DB:" print condition.value print "Deserialized cat:" print "name:", loaded_cat.name print "mice_eaten:", loaded_cat.mice_eaten