日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > linux >内容正文

linux

Linux dbm轻量级数据库介绍与使用

發(fā)布時(shí)間:2023/12/15 linux 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Linux dbm轻量级数据库介绍与使用 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Ubuntu版本:ubuntu-gnome-16.04-desktop-amd64,gnome版

-----------------------------------------------------------------------------------

?

dmb是一個(gè)輕量級(jí)的數(shù)據(jù)庫(kù),但是不是標(biāo)準(zhǔn)的數(shù)據(jù)庫(kù),純粹以二進(jìn)制存儲(chǔ)的一種數(shù)據(jù)庫(kù),常用于系統(tǒng)底層的數(shù)據(jù)庫(kù),在其他一些很少更新內(nèi)容的地方也可以使用,查詢和取出速度非常快但是存入或修改通常較慢,也可以理解,因?yàn)椴幌駝e的數(shù)據(jù)庫(kù),給每個(gè)字段都規(guī)定了大小,給每條記錄都留下了相同大小的空間,那么后來(lái)怎么修改都只是改數(shù)據(jù)庫(kù)文件的一小塊,而dbm如果修改了記錄就只能將整個(gè)文件更新。

?

1. dbm安裝

sudo apt-get install libgdbm-dev

?

2. 編譯

1)源文件中包含頭文件:gdbm-ndbm.h

2)gcc dbm.c -I/usr/include/gdbm -lgdbm_compat -lgdbm

注:不同的Linux發(fā)行版本,頭文件以及編譯方式不一致

?

3. 源碼:?dbm.c

#include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <fcntl.h>//#include <ndbm.h> // On some systems you need to replace the above with #include <gdbm-ndbm.h>#include <string.h>#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); }

?

?

總結(jié)

以上是生活随笔為你收集整理的Linux dbm轻量级数据库介绍与使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。