*I;Berkeley DB Reference Guide: Simple Tutorial[P5

Berkeley DB Reference Guide: Simple Tutorial



(

Opening a database



jOpening a database is done using the Berkeley DB db_open interface.

SThe db_open interface takes seven arguments:

:

file
The name of the database file to be opened.,

type
The type of database to open.HThis value will be one of the three access methods Berkeley DB supports:FDB_BTREE, DB_HASH, or DB_RECNO, or the special value DB_UNKNOWN, which=allows you to open an existing file without knowing its type.i

flags
Various flags that modify the behavior of db_open.EIn our simple case, we care only about one of these flags: DB_CREATE.GThis flag behaves similarly to the POSIX 1003.1 flag to the open systemJcall, causing Berkeley DB to create the underlying database if it does not yet exist.u

mode
The file mode of any underlying files that db_open will create.HThe mode behaves similarly to the POSIX 1003.1 mode argument to the openEsystem call, and specifies an octal value for read, write and executeApermission. Obviously, all we care about is reading and writing!U

dbenv
Additional arguments describing the database environment in which thedatabase will be opened.HThis argument isn't applicable now, and so we will leave it unspecified.R

dbinfo
Additional information describing the database file to be opened.EThis argument is used to specify things like the underlying page size.or a comparison function for B+tree databases.DWe don't need to specify any of these things for our simple example,$and so we will leave it unspecified.x

dbpp
A location in which to store the database handle that db_open willreturn on success.


WHere's what the code to call db_open looks like:+



@As you can see, we're opening a database named access.db.IThe underlying database is a B+tree, and since we've specified DB_CREATE,Fwe'll create it if it doesn't already exist. The mode of any files weHcreate is 0664 (i.e., readable and writeable by the owner and the group,and readable by everyone else).

dIf the call to db_open is successful, the variable dbpIwill contain a database handle that will be used to access the underlying database.

PAMÿÿ