linux dbm数据库,linux dbm数据库
大多數(shù)主流的Linux發(fā)行版都會默認(rèn)安裝gdbm,但在一些發(fā)行版中,你可能需要使用軟件包管理器來安裝相應(yīng)的開發(fā)庫。例如,在
ubuntu中,你可能需要使用Synaptic軟件包管理器來安裝libgdbm-dev軟件包,因?yàn)樗话悴粫荒J(rèn)安裝。
dbm的數(shù)據(jù)塊datum是一個用typedef語句定義的類型。它至少包含下面兩個成員:
void *dptr;
size_t dsize;
dbm訪問函數(shù)包括下面四個:
#include
DBM *dbm_open(const char *filename, int file_open_flags , mode_t mode);
int dbm_store(DBM *database_descriptor , datum key, datum content, int store_mode);
datum datum_fetch(DBM *database_descriptor , datum key);
void dbm_close(DBM *database_descriptor);
1.dbm_open函數(shù)
這個函數(shù)用來打開已有的數(shù)據(jù)庫,也可以用來創(chuàng)建新數(shù)據(jù)庫。filename參數(shù)是一個基本文件名,它不包含.dir或.pag后綴。
其余的參數(shù)與open函數(shù)的第二個和第三個參數(shù)一樣。
dbm_open返回一個指向DBM類型的指針。它被用于所有后續(xù)對數(shù)據(jù)庫的訪問,如果失敗,它將返回(DBM *)0。
2.dbm_store函數(shù)
你用這個函數(shù)把數(shù)據(jù)存儲到數(shù)據(jù)庫中,如前所述,所有數(shù)據(jù)在存儲時都必須有一個唯一的索引。
為了定義你想要存儲的數(shù)據(jù)和用來應(yīng)用他的索引,你必須設(shè)置兩個datum類型的參數(shù):一個用于引用索引,一個用于實(shí)際
數(shù)據(jù)。最后一個參數(shù)store_mode用于控制當(dāng)試圖以一個已有的關(guān)鍵字來存儲數(shù)據(jù)時發(fā)生的情況。如果它被設(shè)置為dbm_insert
,存儲操作將失敗并且dbm_store返回1.如果它被設(shè)置為dbm_replace,則新數(shù)據(jù)將覆蓋已有數(shù)據(jù)并且dbm_store返回0.當(dāng)發(fā)生
其他錯誤時,dbm_store將返回一個負(fù)值。
3.dbm_fetch函數(shù)
dbm_fetch函數(shù)用于從數(shù)據(jù)庫中檢索數(shù)據(jù),它使用一個先前dbm_open調(diào)用返回的指針和一個指向關(guān)鍵字datum類型結(jié)構(gòu)作為參數(shù)。
它返回一個datum類型的結(jié)構(gòu)。如果在數(shù)據(jù)庫中找到與這個與這個關(guān)鍵字關(guān)聯(lián)的數(shù)據(jù),但會的datum結(jié)構(gòu)的dptr和dsize成員的
值將被設(shè)為相應(yīng)數(shù)據(jù)的值。如果沒有找到關(guān)鍵字,dptr將被設(shè)置為NULL;
4.dbm_close函數(shù)
這個函數(shù)用于關(guān)閉dbm_open函數(shù)打開的數(shù)據(jù)庫。它的參數(shù)是先前dbm_open調(diào)用返回的dbm指針。
#include
#include
#include
#include
#include
/* On some systems you need to replace the above with
#include
*/
#include
#define TEST_DB_FILE "/tmp/dbm1_test"
#define ITEMS_USED 3
/* A struct to use to test dbm */
struct test_data {
char misc_chars[15];
int any_integer;
char more_chars[21];
};
int main() {
struct test_data items_to_store[ITEMS_USED];
struct test_data item_retrieved;
char key_to_use[20];
int i, result;
datum key_datum;
datum data_datum;
DBM *dbm_ptr;
dbm_ptr = dbm_open(TEST_DB_FILE, O_RDWR | O_CREAT, 0666);
if (!dbm_ptr) {
fprintf(stderr, "Failed to open database\n");
exit(EXIT_FAILURE);
}
/* put some data in the structures */
memset(items_to_store, '\0', sizeof(items_to_store));
strcpy(items_to_store[0].misc_chars, "First!");
items_to_store[0].any_integer = 47;
strcpy(items_to_store[0].more_chars, "foo");
strcpy(items_to_store[1].misc_chars, "bar");
items_to_store[1].any_integer = 13;
strcpy(items_to_store[1].more_chars, "unlucky?");
strcpy(items_to_store[2].misc_chars, "Third");
items_to_store[2].any_integer = 3;
strcpy(items_to_store[2].more_chars, "baz");
for (i = 0; i < ITEMS_USED; i++) {
/* build a key to use */
sprintf(key_to_use, "%c%c%d",
items_to_store[i].misc_chars[0],
items_to_store[i].more_chars[0],
items_to_store[i].any_integer);
/* build the key datum strcture */
key_datum.dptr = (void *)key_to_use;
key_datum.dsize = strlen(key_to_use);
data_datum.dptr = (void *)&items_to_store[i];
data_datum.dsize = sizeof(struct test_data);
result = dbm_store(dbm_ptr, key_datum, data_datum, DBM_REPLACE);
if (result != 0) {
fprintf(stderr, "dbm_store failed on key %s\n", key_to_use);
exit(2);
}
} /* for */
/* now try and retrieve some data */
sprintf(key_to_use, "bu%d", 13); /* this is the key for the second item */
key_datum.dptr = key_to_use; /*提供測試*/
key_datum.dsize = strlen(key_to_use);
data_datum = dbm_fetch(dbm_ptr, key_datum);
if (data_datum.dptr) {
printf("Data retrieved\n");
memcpy(&item_retrieved, data_datum.dptr, data_datum.dsize);
printf("Retrieved item - %s %d %s\n",
item_retrieved.misc_chars,
item_retrieved.any_integer,
item_retrieved.more_chars);
}
else {
printf("No data found for key %s\n", key_to_use);
}
dbm_close(dbm_ptr);
exit(EXIT_SUCCESS);
}
其他dbm函數(shù)
int dbm_delete(DBM *database_descriptor , datum key);
這個函數(shù)用于從數(shù)據(jù)庫中刪除數(shù)據(jù)項(xiàng),與dbm_fetch一樣,它也使用一個指向關(guān)鍵字的datum類型結(jié)構(gòu)作為其參數(shù),但不同的是,它是用于
刪除數(shù)據(jù)而不是用于檢索數(shù)據(jù)。它在成功時返回0.
int dbm_error(DBM *database_descriptor );
函數(shù)用于測試數(shù)據(jù)庫中是否有錯誤發(fā)生,如果沒有就返回0.
int dbm_clearerr(DBM *database_descriptor);
函數(shù)用于清除數(shù)據(jù)庫中所有已被置位的錯誤條件標(biāo)志。
datum dbm_firstkey(DBM *database_descriptor);
datum dbm_nextkey(DBM *database_descriptor);
這個兩個函數(shù)一般成對來對數(shù)據(jù)庫中的所有關(guān)鍵字進(jìn)行掃描。他們需要的循環(huán)結(jié)構(gòu)如下所示:
DBM *db_ptr;
datum key;
for(key=dbm_firstkey(db_ptr); key.dptr ; key = dbm_nextkey(db_ptr));
#include
#include
#include
#include
#include
/* On some systems you need to replace the above with
#include
*/
#include
#define TEST_DB_FILE "/tmp/dbm2_test"
#define ITEMS_USED 3
/* A struct to use to test dbm */
struct test_data {
char misc_chars[15];
int any_integer;
char more_chars[21];
};
int main() {
struct test_data items_to_store[ITEMS_USED];
struct test_data item_retrieved;
char key_to_use[20];
int i, result;
datum key_datum;
datum data_datum;
DBM *dbm_ptr;
dbm_ptr = dbm_open(TEST_DB_FILE, O_RDWR | O_CREAT, 0666);
if (!dbm_ptr) {
fprintf(stderr, "Failed to open database\n");
exit(EXIT_FAILURE);
}
/* put some data in the structures */
memset(items_to_store, '\0', sizeof(items_to_store));
strcpy(items_to_store[0].misc_chars, "First!");
items_to_store[0].any_integer = 47;
strcpy(items_to_store[0].more_chars, "foo");
strcpy(items_to_store[1].misc_chars, "bar");
items_to_store[1].any_integer = 13;
strcpy(items_to_store[1].more_chars, "unlucky?");
strcpy(items_to_store[2].misc_chars, "Third");
items_to_store[2].any_integer = 3;
strcpy(items_to_store[2].more_chars, "baz");
for (i = 0; i < ITEMS_USED; i++) {
/* build a key to use */
sprintf(key_to_use, "%c%c%d",
items_to_store[i].misc_chars[0],
items_to_store[i].more_chars[0],
items_to_store[i].any_integer);
/* build the key datum strcture */
key_datum.dptr = key_to_use;
key_datum.dsize = strlen(key_to_use);
data_datum.dptr = (void *)&items_to_store[i];
data_datum.dsize = sizeof(struct test_data);
result = dbm_store(dbm_ptr, key_datum, data_datum, DBM_REPLACE);
if (result != 0) {
fprintf(stderr, "dbm_store failed on key %s\n", key_to_use);
exit(2);
}
} /* for */
/* now try and delete some data */
sprintf(key_to_use, "bu%d", 13); /* this is the key for the second item */
key_datum.dptr = key_to_use;
key_datum.dsize = strlen(key_to_use);
if (dbm_delete(dbm_ptr, key_datum) == 0) {
printf("Data with key %s deleted\n", key_to_use);
}
else {
printf("Nothing deleted for key %s\n", key_to_use);
}
for (key_datum = dbm_firstkey(dbm_ptr);
key_datum.dptr;
key_datum = dbm_nextkey(dbm_ptr)) {
data_datum = dbm_fetch(dbm_ptr, key_datum);
if (data_datum.dptr) {
printf("Data retrieved\n");
memcpy(&item_retrieved, data_datum.dptr, data_datum.dsize);
printf("Retrieved item - %s %d %s\n",
item_retrieved.misc_chars,
item_retrieved.any_integer,
item_retrieved.more_chars);
}
else {
printf("Woops - no data found for key %s\n", key_to_use);
}
} /* for each key */
dbm_close(dbm_ptr);
exit(EXIT_SUCCESS);
}
總結(jié)
以上是生活随笔為你收集整理的linux dbm数据库,linux dbm数据库的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: git pull git push 报s
- 下一篇: 知识表示的方法(1)——产生式表示法