我是数据库的新手,我编写了这个练习程序来测试Berkeley BDB。我在打开环境时遇到了麻烦 - 错误出现在我的控制台中并说:
testEnv\_db.001: No such file or directory
Error opening database environment
DbEnv::open: No such file or directory
控制台程序的代码完全基于Berkeley DB Transactions指南中的示例,这里是int main()
下的错误之前的程序文本。
String^ key_allkeys = "_ALLKEYS";
String^ key_valcount = "_COUNT";
// 1. Print some information to the screen, prompt user to enter path for key-value pair location:
Console::WriteLine("Oracle Berkeley DB Example Database Creator");
Console::WriteLine();
Console::WriteLine("Enter the path to the text file of key-value pairs:");
String^ kv_path = Console::ReadLine();
// 2. Declare initial variables:
String^ totalkeys;
int totalval;
// 3. Open the database:
u_int32_t env_flags = DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN;
std::string envHome("testEnv");
u_int32_t db_flags = DB_CREATE | DB_AUTO_COMMIT;
Db *dbp = NULL;
const char *file_name = "mydb.db";
DbEnv myEnv(0);
try
{
myEnv.open(envHome.c_str(), env_flags, 0);
dbp = new Db(&myEnv, 0);
dbp->open(NULL, file_name, NULL, DB_BTREE, db_flags, 0);
}
catch(DbException &e)
{
std::cerr << "Error opening database environment: " << std::endl;
std::cerr << e.what() << std::endl;
exc_block(); // Block
return (EXIT_FAILURE);
}
很明显,程序在try
/ catch
块周围失败了。我已经阅读了手册(字面意思是RTFM)而我无法弄明白 - 为环境和数据库指定了DB_CREATE
标志,所以这里不应该有问题。
有什么想法吗?
答案 0 :(得分:0)
可能只是testEnv目录不存在于运行程序的目录中?您必须自己创建环境主目录。
另外,您可能希望在打开环境时指定DB_RECOVER。