dbTalk Databases Forums  

Q:why didn't the following code creat the "mima.db"

comp.databases.berkeley-db comp.databases.berkeley-db


Discuss Q:why didn't the following code creat the "mima.db" in the comp.databases.berkeley-db forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
lwu321 (Offline)
Junior Member
 
Posts: 6
Join Date: Apr 2006

Default Q:why didn't the following code creat the "mima.db" - 04-16-2006 , 12:49 AM






note:there's no compile,build,run error!and these codes are build in VC++ 6.0 in windows


here's the code:
//mima.h
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//only this head should include for use bdb.
#include "db_cxx.h"
#include "db.h"
class Mima
{
private:
Db mimadb;
//u_int32_t oFlags;
//Mima() : mimadb(NULL, 0) {}
void close();
char *mima;
Dbt pkey;
Dbt pdata;
char *yonghuming;
public:
Mima();
// Our destructor just calls our private close method.
~Mima() { close(); }
//写入密码
// put();//不需要写入操作
//读出密码
get();
//修改密码//修改的时候先删除密码,再写入操作
//xiugai(str::xinmima);
};

//mima.cpp
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//only this head should include for use bdb.
#include "mima.h"


//implementation for the constructor
Mima :: Mima()
: mimadb(NULL, 0), // Instantiate Db object
//oFlags(DB_CREATE),
pkey(&mima, sizeof(char)),
pdata(yonghuming, strlen(yonghuming) + 1)
// mima(123456)
{

try
{
mimadb.open(NULL, // Transaction pointer
"mima.db", // Database file name
NULL, // Optional logical database name
DB_BTREE, // Database access method
DB_CREATE, // Open flags
0); // File mode (using defaults)

}
catch(DbException &e)
{
// Error handling code goes here
std::cerr << e.what() << std::endl;
}
catch(std::exception &e)
{
// Error handling code goes here
std::cerr << e.what() << std::endl;
}
char *yonghuming = "lwu";
char *mima="123456";

mimadb.put(NULL, &pkey, &pdata, DB_NOOVERWRITE);//初始化密码"123456"
printf("db: %s: data stored.\n", pdata.get_data());
}

void Mima::close()
{
// Close the mimadb
try
{
mimadb.close(0);
// DbException is not subclassed from std::exception, so
// need to catch both of these.
}
catch(DbException &e)
{
std::cerr << e.what() << std::endl;
}
catch(std::exception &e)
{
std::cerr << e.what() << std::endl;
}

}
//Mima:: put()
//{
// char *yonghuming = "lwu";
// mima="123456";
// Dbt pkey(&mima, sizeof(char));
// Dbt pdata(yonghuming, strlen(yonghuming) + 1);
// mimadb.put(NULL, &pkey, &pdata, DB_NOOVERWRITE);
//}
Mima::get()
{
mimadb.get(NULL, &pkey, &pdata, 0);

}

void main()
{
Mima a();


}

//codes end
thanks great master.

Reply With Quote
  #2  
Old   
Alex
 
Posts: n/a

Default Re: Q:why didn't the following code creat the "mima.db" - 04-17-2006 , 05:07 PM






The first problem is that the constructor is not being called. Your
main should probably be:

void main()
{
Mima a;
}

Not
void main()
{
Mima a();
}

Then you are using the "yonghuming" and "mima" member variables in the
constructor prior to initializing them.


Reply With Quote
  #3  
Old   
lwu321 (Offline)
Junior Member
 
Posts: 6
Join Date: Apr 2006

Thumbs up thanks - 04-18-2006 , 10:23 AM



thank you Alex!

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.