insert

Given a table, you can insert records

  1. >>> db.person.insert(name="Alex")
  2. 1
  3. >>> db.person.insert(name="Bob")
  4. 2

Insert returns the unique “id” value of each record inserted.

You can truncate the table, i.e., delete all records and reset the counter of the id.

  1. >>> db.person.truncate()

Now, if you insert a record again, the counter starts again at 1 (this is back-end specific and does not apply to Google NoSQL):

  1. >>> db.person.insert(name="Alex")
  2. 1

Notice you can pass a parameter to truncate, for example you can tell SQLite to restart the id counter.

  1. >>> db.person.truncate('RESTART IDENTITY CASCADE')

The argument is in raw SQL and therefore engine specific.

web2py also provides a bulk_insert method

  1. >>> db.person.bulk_insert([{'name': 'Alex'}, {'name': 'John'}, {'name': 'Tim'}])
  2. [3, 4, 5]

It takes a list of dictionaries of fields to be inserted and performs multiple inserts at once. It returns the list of “id” values of the inserted records. On the supported relational databases there is no advantage in using this function as opposed to looping and performing individual inserts but on Google App Engine NoSQL, there is a major speed advantage.