📄 bdb3.cpp
字号:
#include "db.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
//#define MAXDATABUF 1024
//#define MAXFIELD 20
//#define MAXLINE 150
DB *dbp;
u_int32_t open_flags;
void show_inventory_item(void *vBuf);
void show_records()
{
int count=0;
DBC *cursorp;
DBT key, data;
int ret;
// / Initialize our DBTs.
memset(&key, 0, sizeof(DBT));
memset(&data, 0, sizeof(DBT));
// /Get a cursor to the itemname db
dbp->cursor(dbp, NULL,&cursorp, 0);
while ((ret =cursorp->c_get(cursorp, &key, &data, DB_NEXT)) == 0)
{
// show_inventory_item(data.data);
count++;
}
// Close the cursor
if( cursorp!=NULL)
cursorp->c_close(cursorp);
printf("\nThe total record is %d \n",count);
}
/*
void show_records1()
{
DBC *itemname_cursorp;
DBT key, data;
int ret, exit_value;
// Initialize our DBTs.
memset(&key, 0, sizeof(DBT));
memset(&data, 0, sizeof(DBT));
// Get a cursor to the itemname db
dbp->cursor(dbp, NULL,&itemname_cursorp, 0);
key.data = "ables";
// printf("The key is %s\n",key.data);
key.size = (u_int32_t)strlen("ables") + 1;
data.flags=DB_DBT_USERMEM;
dbp->get(dbp,0,&key,&data,0);
printf("The key is %s\n",key.data);
printf("The key is %s\n",data.data);
//show_inventory_item(data.data);
exit_value = 0;
ret = itemname_cursorp->c_get(itemname_cursorp, &key, &data, DB_SET_RANGE);
if (!ret) {
do {
printf("The key is %s\n",key.data);
show_inventory_item(data.data);
} while (itemname_cursorp->c_get(itemname_cursorp, &key, &data,DB_NEXT_DUP) == 0);
} else {
printf("No records found for '%s'\n", "OranfruiRu6Gh");
}
// itemname_cursorp->c_close(itemname_cursorp);
// return (exit_value);
}
*/
/*
* Shows an inventory item. How we retrieve the inventory
* item values from the provided buffer is strictly dependent
* on the order that those items were originally stored in the
* DBT. See load_inventory_database in example_database_load
* for how this was done.
*/
void show_inventory_item(void *vBuf)
{
float price;
int quantity;
size_t buf_pos;
char *category, *name, *sku, *vendor_name;
char *buf = (char *)vBuf;
price = *((float *)buf);
buf_pos = sizeof(float);
quantity = *((int *)(buf + buf_pos));
buf_pos += sizeof(int);
name = buf + buf_pos;
buf_pos += strlen(name) + 1;
sku = buf + buf_pos;
buf_pos += strlen(sku) + 1;
category = buf + buf_pos;
buf_pos += strlen(category) + 1;
vendor_name = buf + buf_pos;
printf("name: %s\n", name);
printf("\tSKU: %s\n", sku);
printf("\tCategory: %s\n", category);
printf("\tPrice: %.2f\n", price);
printf("\tQuantity: %i\n", quantity);
printf("\tVendor:%s\n",vendor_name);
}
int main()
{
int ret;
clock_t start,end;
ret = db_create(&dbp, NULL, 0);
if (ret != 0) {
fprintf(stderr, "%s: \n",db_strerror(ret));
return (ret);
}
open_flags = DB_CREATE; /* Allow database creation */
/* Now open the database */
ret = dbp->open(dbp, /* Pointer to the database */
NULL, /* Txn pointer */
"zieckey.db", /* File name */
NULL, /* Logical db name */
DB_BTREE, /* Database type (using btree) */
open_flags, /* Open flags */
0); /* File mode. Using defaults */
if (ret != 0) {
dbp->err(dbp, ret, "Database '%s' open failed.", "zieckey.db");
return (ret);
}
start=clock();
show_records();
end=clock();
printf("\nThe query time is %f\n",(double)(end-start)/CLOCKS_PER_SEC);
printf("test2\n");
if (dbp != NULL)
{
printf("test3\n");
// ret = dbp->close(dbp, 0);
printf("test1\n");
//if (ret != 0)
// fprintf(stderr, "Inventory database close failed: %s\n",db_strerror(ret));
// exit(0) ;
}
printf("test4\n");
exit(0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -