We're going to focus on two small code samples, each providing some insight into dbglue. In the first example, we are going to fetch a collection of rows from the database, modify them, and commit them. The second example will fetch a collection of rows using a like query, limiting our results, modifying one and committing. Finally, the third example will handle an insert.

Example 1 - Inserting

Our final example does a database insert. Notice how an insert is identical to an update. The basic idea is that a row should know whether or not it's new, hence handling the commit.

table = p.table('my_table')
row = table.new()
row.colour = 'blue'
row.save()


Example 2 - Fetching Everything

Assuming that p is a valid database connection, the following can be used to fetch rows:

table = p.table('my_table')
results = table.find() # this finds all rows in the table


Assuming once again, that the table has a column named "colour", the following will loop through all table results, change the colour to blue and then commit the results:

for _r in results:
_r.colour = "blue"
_r.save()

Example 3 - Selective Fetching

Now let's do a like query with a limit. We search for the first five rows in the database with a colour like red. Then, we change them to blue, and save.

table = p.table('my_table')
results = table.find(limit=5, like=true, where={'colour' : 'red'})
for _r in results:
_r.colour = "blue"
_r.save()