linux dbm数据库,linux dbm数据库
大多數(shù)主流的Linux發(fā)行版都會(huì)默認(rèn)安裝gdbm,但在一些發(fā)行版中,你可能需要使用軟件包管理器來(lái)安裝相應(yīng)的開(kāi)發(fā)庫(kù)。例如,在
ubuntu中,你可能需要使用Synaptic軟件包管理器來(lái)安裝libgdbm-dev軟件包,因?yàn)樗话悴粫?huì)被默認(rèn)安裝。
dbm的數(shù)據(jù)塊datum是一個(gè)用typedef語(yǔ)句定義的類(lèi)型。它至少包含下面兩個(gè)成員:
void *dptr;
size_t dsize;
dbm訪問(wèn)函數(shù)包括下面四個(gè):
#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ù)
這個(gè)函數(shù)用來(lái)打開(kāi)已有的數(shù)據(jù)庫(kù),也可以用來(lái)創(chuàng)建新數(shù)據(jù)庫(kù)。filename參數(shù)是一個(gè)基本文件名,它不包含.dir或.pag后綴。
其余的參數(shù)與open函數(shù)的第二個(gè)和第三個(gè)參數(shù)一樣。
dbm_open返回一個(gè)指向DBM類(lèi)型的指針。它被用于所有后續(xù)對(duì)數(shù)據(jù)庫(kù)的訪問(wèn),如果失敗,它將返回(DBM *)0。
2.dbm_store函數(shù)
你用這個(gè)函數(shù)把數(shù)據(jù)存儲(chǔ)到數(shù)據(jù)庫(kù)中,如前所述,所有數(shù)據(jù)在存儲(chǔ)時(shí)都必須有一個(gè)唯一的索引。
為了定義你想要存儲(chǔ)的數(shù)據(jù)和用來(lái)應(yīng)用他的索引,你必須設(shè)置兩個(gè)datum類(lèi)型的參數(shù):一個(gè)用于引用索引,一個(gè)用于實(shí)際
數(shù)據(jù)。最后一個(gè)參數(shù)store_mode用于控制當(dāng)試圖以一個(gè)已有的關(guān)鍵字來(lái)存儲(chǔ)數(shù)據(jù)時(shí)發(fā)生的情況。如果它被設(shè)置為dbm_insert
,存儲(chǔ)操作將失敗并且dbm_store返回1.如果它被設(shè)置為dbm_replace,則新數(shù)據(jù)將覆蓋已有數(shù)據(jù)并且dbm_store返回0.當(dāng)發(fā)生
其他錯(cuò)誤時(shí),dbm_store將返回一個(gè)負(fù)值。
3.dbm_fetch函數(shù)
dbm_fetch函數(shù)用于從數(shù)據(jù)庫(kù)中檢索數(shù)據(jù),它使用一個(gè)先前dbm_open調(diào)用返回的指針和一個(gè)指向關(guān)鍵字datum類(lèi)型結(jié)構(gòu)作為參數(shù)。
它返回一個(gè)datum類(lèi)型的結(jié)構(gòu)。如果在數(shù)據(jù)庫(kù)中找到與這個(gè)與這個(gè)關(guān)鍵字關(guān)聯(lián)的數(shù)據(jù),但會(huì)的datum結(jié)構(gòu)的dptr和dsize成員的
值將被設(shè)為相應(yīng)數(shù)據(jù)的值。如果沒(méi)有找到關(guān)鍵字,dptr將被設(shè)置為NULL;
4.dbm_close函數(shù)
這個(gè)函數(shù)用于關(guān)閉dbm_open函數(shù)打開(kāi)的數(shù)據(jù)庫(kù)。它的參數(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; /*提供測(cè)試*/
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);
這個(gè)函數(shù)用于從數(shù)據(jù)庫(kù)中刪除數(shù)據(jù)項(xiàng),與dbm_fetch一樣,它也使用一個(gè)指向關(guān)鍵字的datum類(lèi)型結(jié)構(gòu)作為其參數(shù),但不同的是,它是用于
刪除數(shù)據(jù)而不是用于檢索數(shù)據(jù)。它在成功時(shí)返回0.
int dbm_error(DBM *database_descriptor );
函數(shù)用于測(cè)試數(shù)據(jù)庫(kù)中是否有錯(cuò)誤發(fā)生,如果沒(méi)有就返回0.
int dbm_clearerr(DBM *database_descriptor);
函數(shù)用于清除數(shù)據(jù)庫(kù)中所有已被置位的錯(cuò)誤條件標(biāo)志。
datum dbm_firstkey(DBM *database_descriptor);
datum dbm_nextkey(DBM *database_descriptor);
這個(gè)兩個(gè)函數(shù)一般成對(duì)來(lái)對(duì)數(shù)據(jù)庫(kù)中的所有關(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数据库的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: git pull git push 报s
- 下一篇: linux 其他常用命令