gpt4 book ai didi

c - 如何使用 C 在 Berkeley DB 中将整数值分配给 "key.data"

转载 作者:太空宇宙 更新时间:2023-11-04 03:04:34 25 4
gpt4 key购买 nike

最近我正在使用 Berkeley DB。我见过一些例子,其中人们在使用 Berkeley DB 创建数据库时使用“string”作为“key.data”的值。我想为它分配一个整数值。我怎样才能做到这一点?我应该创建一个包含 int 成员的结构还是有其他可能的方法?

DBT key, data;
memset(&key, 0, sizeof(key));
memset(&data, 0, sizeof(data));
key.data = "fruit";
key.size = sizeof("fruit");

所以我想分配一个整数值,而不是上面的“水果”。任何形式的帮助将不胜感激。

最佳答案

DBT structures provide a void * field that you use to point to your data, and another field that identifies the data length. They can therefore be used to store anything from simple primitive data to complex structures so long as the information you want to store resides in a single contiguous block of memory.

参见,http://download.oracle.com/docs/cd/E17076_02/html/gsg/C/DBEntry.html

要存储整数,您可以将指向 int 的指针分配给 key.data,例如:

int x = 42;
key.data = &x;
key.size = sizeof(x);

关于c - 如何使用 C 在 Berkeley DB 中将整数值分配给 "key.data",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7195021/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com