挿入

□未翻訳

□翻訳中

■翻訳完了(細田謙二)

■レビュー(Omi Chiba)

挿入

テーブルが与えられると、レコードを挿入することができます:

Given a table, you can insert records

1.

2.

3.

4.

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

1

>>> db.person.insert(name="Bob")

2

挿入によって、各挿入したレコードのユニークな"id"値が返されます。

Insert returns the unique "id" value of each record inserted.

テーブルを切捨てることができます。つまり、すべてのレコードを削除し、idのカウンタを元に戻します。

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

1.

>>> db.person.truncate()

このとき、もう一度レコードを挿入した場合、カウンタは1から始まります(これはバックエンド固有で、GAEには適用されません):

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

1.

2.

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

1

web2pyはまたbulk_insertメソッドを用意しています。

web2py also provides a bulk_insert method

1.

2.

>>> db.person.bulk_insert([{'name':'Alex'}, {'name':'John'}, {'name':'Tim'}])

[3,4,5]

これは、挿入されるフィールドの辞書のリストを受け取り、複数の挿入を一度に実行します。そして挿入された複数のレコードのIDを返します。サポートされているリレーショナルデータベースでは、この関数を使用しても、ループさせて個別に挿入を実行する場合に比べて、特に利点はありません。しかし、Google App Engineでは、大幅な高速化が見込めます。

It takes a list of dictionaries of fields to be inserted and performs multiple inserts at once. It returns the IDs 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, there is a major speed advantage.