Berkeley DB Reference Version 5.3.33

DB->put()

#include <db.h>

int
DB->put(DB *db,
    DB_TXN *txnid, DBT *key, DBT *data, u_int32_t flags);  

The DB->put() method stores key/data pairs in the database. The default behavior of the DB->put() function is to enter the new key/data pair, replacing any previously existing key if duplicates are disallowed, or adding a duplicate data item if duplicates are allowed. If the database supports duplicates, the DB->put() method adds the new data value at the end of the duplicate set. If the database supports sorted duplicates, the new data value is inserted at the correct sorted location.

Unless otherwise specified, the DB->put() method returns a non-zero error value on failure and 0 on success.

Parameters

txnid

If the operation is part of an application-specified transaction, the txnid parameter is a transaction handle returned from DB_ENV->txn_begin(); if the operation is part of a Berkeley DB Concurrent Data Store group, the txnid parameter is a handle returned from DB_ENV->cdsgroup_begin(); otherwise NULL. If no transaction handle is specified, but the operation occurs in a transactional database, the operation will be implicitly transaction protected.

key

The key DBT operated on.

If creating a new record in a Heap database, the key DBT must be empty. The put method will return the new record's Record ID (RID) in the key DBT.

data

The data DBT operated on.

flags

The flags parameter must be set to 0 or one of the following values:

Errors

The DB->put() method may fail and return one of the following non-zero errors:

DB_FOREIGN_CONFLICT

A foreign key constraint violation has occurred. This can be caused by one of two things:

  1. An attempt was made to add a record to a constrained database, and the key used for that record does not exist in the foreign key database.

  2. DB_FOREIGN_ABORT was declared for a foreign key database, and then subsequently a record was deleted from the foreign key database without first removing it from the constrained secondary database.

DB_HEAP_FULL

An attempt was made to add or update a record in a Heap database. However, the size of the database was constrained using the DB->set_heapsize() method, and that limit has been reached.

DB_LOCK_DEADLOCK

A transactional database environment operation was selected to resolve a deadlock.

DB_LOCK_NOTGRANTED

A Berkeley DB Concurrent Data Store database environment configured for lock timeouts was unable to grant a lock in the allowed time.

You attempted to open a database handle that is configured for no waiting exclusive locking, but the exclusive lock could not be immediately obtained. See DB->set_lk_exclusive() for more information.

DB_REP_HANDLE_DEAD

When a client synchronizes with the master, it is possible for committed transactions to be rolled back. This invalidates all the database and cursor handles opened in the replication environment. Once this occurs, an attempt to use such a handle will return DB_REP_HANDLE_DEAD. The application will need to discard the handle and open a new one in order to continue processing.

DB_REP_LOCKOUT

The operation was blocked by client/master synchronization.

EACCES

An attempt was made to modify a read-only database.

EINVAL

If a record number of 0 was specified; an attempt was made to add a record to a fixed-length database that was too large to fit; an attempt was made to do a partial put; an attempt was made to add a record to a secondary index; or if an invalid flag value or parameter was specified.

ENOSPC

A btree exceeded the maximum btree depth (255).

Class

DB

See Also

Database and Related Methods