Writing a new row to a table in the store, and updating an existing row are usually identical operations (although methods exist that work only if the row is being updated, or only if it is being created — these are described a little later in this section).
Remember that you can only write data to a table after it has been added to the store. See Introducing Oracle NoSQL Database Tables and Indexes for details.
To write a row to a table in the store:
Create a store handle and open it.
Use a dictionary to describe the row. Each key in the dictionary must correspond to a field name that has been declared for the table to which you will write the row.
Use the Store.put()
method to
write the row to the store. This method has two
required arguments. The first identifies the table to
which you want to write the row. The second accepts the
dictionary you constructed in the previous step.
For example:
from nosqldb import Factory from nosqldb import IllegalArgumentException from nosqldb import ProxyConfig from nosqldb import StoreConfig import logging import os # locations where our store and proxy can be found kvlite = 'localhost:5000' proxy = 'localhost:7010' kvstoreconfig = StoreConfig('kvstore', [kvlite]) store = Factory.open(proxy, kvstoreconfig) row_d = { 'item' : 'bolts', 'description' : "Hex head, stainless", 'count' : 5, 'percentage' : 0.2173913} try: store.put("myTable", row_d) logging.debug("Store write succeeded.") except IllegalArgumentException, iae: logging.error("Could not write table.") logging.error(iae.message) store.close()
To write to a child table, first create the row in the parent table to which the child belongs. You do this by populating the parent row with data. Then you write the child table's row(s). When you do, you must specify the primary key used by the parent table, as well as the primary key used by the child table's rows.
For example, in Defining Child Tables we showed how to create a child table. To write data to that table, do this:
from nosqldb import Factory from nosqldb import IllegalArgumentException from nosqldb import ProxyConfig from nosqldb import StoreConfig import logging import os # locations where our store and proxy can be found kvlite = 'localhost:5000' proxy = 'localhost:7010' kvstoreconfig = StoreConfig('kvstore', [kvlite]) store = Factory.open(proxy, kvstoreconfig) parent_d = {'itemCategory' : 'Bolts', 'description' : 'Metric & US sizes'} try: store.put("myInventory", parent_d) logging.debug("Store write succeeded.") except IllegalArgumentException, iae: logging.error("Could not write table.") logging.error(iae.message) child_d = {'itemCategory' : 'Bolts', 'itemSKU' : '1392610', 'itemDescription' : "1/4-20 x 1/2 Grade 8 Hex", 'price' : 11.99, 'inventoryCount' : 1457 child_row= Row(child_d) try: store.put("myInventory.itemDetails", child_row) logging.debug("Store write succeeded.") except IllegalArgumentException, iae: logging.error("Could not write table.") logging.error(iae.message) store.close()
Beyond the very simple usage of the method illustrated above, there are three other put operations that you can use:
Store.put_if_absent()
This method will only put the row if the row's primary key value DOES NOT currently exist in the table. That is, this method is successful only if it results in a create operation.
Store.put_if_present()
This method will only put the row if the row's primary key value already exists in the table. That is, this method is only successful if it results in an update operation.
Store.put_if_version()
This method will put the row only if the value matches the supplied version information. For more information, see Using Row Versions.