Mixed C++/CLI code with Berkeley DB
-
Hi, I'm traying to use Berkeley DB in C++/CLI with /clr mode. I wrote two applications, one will store to a database and another one will try to read the contents of that database, but i failed in doing that! This the first code (first app) for writing to a database:
#include "stdafx.h"
#pragma comment(lib,"libdb51")
using namespace System;
using namespace System::Runtime::InteropServices;int main(array<System::String ^> ^args)
{
Db SigDb(0,0);
unsigned int oFlags= DB_CREATE;
SigDb.open(NULL,"SigDb.db",0,DB_BTREE,oFlags,0);
String^ HexSig="1E81F1C1176434";
wchar_t* a=( wchar_t* )Marshal::StringToHGlobalUni(HexSig).ToPointer() ;
wchar_t* A=( wchar_t* )Marshal::StringToHGlobalUni(HexSig).ToPointer();;Dbt key1(a,100);
Dbt data1(A,100);int ret= SigDb.put(NULL,&key1,&data1, DB_NOOVERWRITE);
if(ret==DB_KEYEXIST){
Console::WriteLine("You are trying to insert an exist key!");
}SigDb.close(0);
Marshal::FreeHGlobal(IntPtr(A));
Marshal::FreeHGlobal(IntPtr(a));
return 0;
}and this is the second code for reading from the database:
#include "stdafx.h"
#pragma comment(lib,"libdb51")
using namespace System;
using namespace System::Runtime::InteropServices;int main(array<System::String ^> ^args)
{
Db SigDb(0,0);
unsigned int oFlags= DB_CREATE;
SigDb.open(NULL,"SigDb.db",0,DB_BTREE,oFlags,0);
String^ HexSig="1E81F1C1176434";
wchar_t* a=( wchar_t* )Marshal::StringToHGlobalUni(HexSig).ToPointer();SigDb.open(NULL,"SigDb.db",0,DB_BTREE,oFlags,0);
wchar_t DDData[200];
Dbt getKey, getData;
getKey.set_data(a);getKey.set_size(100);
getData.set_data(DDData);
getData.set_ulen(200);
getData.set_flags(DB_DBT_USERMEM);if(SigDb.get(NULL,&getKey,&getData,0)==DB_NOTFOUND)
Console::WriteLine("Not Found !");
else
Console::WriteLine(" {0}",Marshal::PtrToStringUni((IntPtr)DDData));Marshal::FreeHGlobal(IntPtr(a));
Console::ReadLine();
return 0;
}always the second app says "Not found"! On the other hand when i tried to write the same data twise in the database using the first code, it says "You are trying to insert an exist key!" and that proves that the data was written! Any idea pls!
-
Hi, I'm traying to use Berkeley DB in C++/CLI with /clr mode. I wrote two applications, one will store to a database and another one will try to read the contents of that database, but i failed in doing that! This the first code (first app) for writing to a database:
#include "stdafx.h"
#pragma comment(lib,"libdb51")
using namespace System;
using namespace System::Runtime::InteropServices;int main(array<System::String ^> ^args)
{
Db SigDb(0,0);
unsigned int oFlags= DB_CREATE;
SigDb.open(NULL,"SigDb.db",0,DB_BTREE,oFlags,0);
String^ HexSig="1E81F1C1176434";
wchar_t* a=( wchar_t* )Marshal::StringToHGlobalUni(HexSig).ToPointer() ;
wchar_t* A=( wchar_t* )Marshal::StringToHGlobalUni(HexSig).ToPointer();;Dbt key1(a,100);
Dbt data1(A,100);int ret= SigDb.put(NULL,&key1,&data1, DB_NOOVERWRITE);
if(ret==DB_KEYEXIST){
Console::WriteLine("You are trying to insert an exist key!");
}SigDb.close(0);
Marshal::FreeHGlobal(IntPtr(A));
Marshal::FreeHGlobal(IntPtr(a));
return 0;
}and this is the second code for reading from the database:
#include "stdafx.h"
#pragma comment(lib,"libdb51")
using namespace System;
using namespace System::Runtime::InteropServices;int main(array<System::String ^> ^args)
{
Db SigDb(0,0);
unsigned int oFlags= DB_CREATE;
SigDb.open(NULL,"SigDb.db",0,DB_BTREE,oFlags,0);
String^ HexSig="1E81F1C1176434";
wchar_t* a=( wchar_t* )Marshal::StringToHGlobalUni(HexSig).ToPointer();SigDb.open(NULL,"SigDb.db",0,DB_BTREE,oFlags,0);
wchar_t DDData[200];
Dbt getKey, getData;
getKey.set_data(a);getKey.set_size(100);
getData.set_data(DDData);
getData.set_ulen(200);
getData.set_flags(DB_DBT_USERMEM);if(SigDb.get(NULL,&getKey,&getData,0)==DB_NOTFOUND)
Console::WriteLine("Not Found !");
else
Console::WriteLine(" {0}",Marshal::PtrToStringUni((IntPtr)DDData));Marshal::FreeHGlobal(IntPtr(a));
Console::ReadLine();
return 0;
}always the second app says "Not found"! On the other hand when i tried to write the same data twise in the database using the first code, it says "You are trying to insert an exist key!" and that proves that the data was written! Any idea pls!
As a guess if the data type in the database is char (and not varchar) then normally databases fill the column. Your usage suggests a column width of 100 but a value that is less than that. A fill character, in my experience, is space. So for your query you must explicitly space fill it yourself.
-
As a guess if the data type in the database is char (and not varchar) then normally databases fill the column. Your usage suggests a column width of 100 but a value that is less than that. A fill character, in my experience, is space. So for your query you must explicitly space fill it yourself.
I am not sure if i understood you. I made the followin in the second code but still same problem:
wchar\_t DDData\[14\]; Dbt getKey, getData; getKey.set\_data(a); getKey.set\_size(HexSig->Length); getData.set\_data(DDData); getData.set\_ulen(HexSig->Length); getData.set\_flags(DB\_DBT\_USERMEM);
-
I am not sure if i understood you. I made the followin in the second code but still same problem:
wchar\_t DDData\[14\]; Dbt getKey, getData; getKey.set\_data(a); getKey.set\_size(HexSig->Length); getData.set\_data(DDData); getData.set\_ulen(HexSig->Length); getData.set\_flags(DB\_DBT\_USERMEM);
-
If the database data type is 'char', and has a size of 5 and you put 'x' in it then to query for that value you need to use 'x ' (x followed by 4 spaces is a size of 5.)
I tried to make the key size (In Query operation) dynamic with "for" loop as the code below.. this was without any benefit:
for (int i=1;i<90000000; i++){
getKey.set_size(i);
getData.set_data(DDData);
getData.set_ulen(28);
getData.set_flags(DB_DBT_USERMEM);
if(SigDb.get(NULL,&getKey,&getData,0)!=DB_NOTFOUND)
Console::WriteLine(" {0},,{1}",Marshal::PtrToStringUni((IntPtr)DDData),i);
}
Marshal::FreeHGlobal(IntPtr(a));
Console::WriteLine("Finish");