insert
Given a table, you can insert records
>>> db.person.insert(name="Alex")
1
>>> db.person.insert(name="Bob")
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.
>>> 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):
>>> db.person.insert(name="Alex")
1
Notice you can pass a parameter to truncate
, for example you can tell SQLite to restart the id counter.
>>> db.person.truncate('RESTART IDENTITY CASCADE')
The argument is in raw SQL and therefore engine specific.
web2py also provides a bulk_insert method
>>> db.person.bulk_insert([{'name': 'Alex'}, {'name': 'John'}, {'name': 'Tim'}])
[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.