2
* See the file LICENSE for redistribution information.
4
* Copyright (c) 1997-2002
5
* Sleepycat Software. All rights reserved.
10
#include <sys/types.h>
31
static const char FileName[];
33
// no need for copy and assignment
34
AccessExample(const AccessExample &);
35
void operator = (const AccessExample &);
40
// Use a try block just to report any errors.
41
// An alternate approach to using exceptions is to
42
// use error models (see DbEnv::set_error_model()) so
43
// that error codes are returned for all Berkeley DB methods.
48
return (EXIT_SUCCESS);
50
catch (DbException &dbe) {
51
cerr << "AccessExample: " << dbe.what() << "\n";
52
return (EXIT_FAILURE);
56
const char AccessExample::FileName[] = "access.db";
58
AccessExample::AccessExample()
62
void AccessExample::run()
64
// Remove the previous database.
65
(void)remove(FileName);
67
// Create the database object.
68
// There is no environment for this simple example.
71
db.set_error_stream(&cerr);
72
db.set_errpfx("AccessExample");
73
db.set_pagesize(1024); /* Page size: 1K. */
74
db.set_cachesize(0, 32 * 1024, 0);
75
db.open(NULL, FileName, NULL, DB_BTREE, DB_CREATE, 0664);
78
// Insert records into the database, where the key is the user
79
// input and the data is the user input in reverse order.
92
cin.getline(buf, sizeof(buf));
96
if ((len = strlen(buf)) <= 0)
98
for (t = rbuf, p = buf + (len - 1); p >= buf;)
102
Dbt key(buf, len + 1);
103
Dbt data(rbuf, len + 1);
105
ret = db.put(0, &key, &data, DB_NOOVERWRITE);
106
if (ret == DB_KEYEXIST) {
107
cout << "Key " << buf << " already exists.\n";
112
// We put a try block around this section of code
113
// to ensure that our database is properly closed
114
// in the event of an error.
117
// Acquire a cursor for the table.
119
db.cursor(NULL, &dbcp, 0);
121
// Walk through the table, printing the key/data pairs.
124
while (dbcp->get(&key, &data, DB_NEXT) == 0) {
125
char *key_string = (char *)key.get_data();
126
char *data_string = (char *)data.get_data();
127
cout << key_string << " : " << data_string << "\n";
131
catch (DbException &dbe) {
132
cerr << "AccessExample: " << dbe.what() << "\n";