Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. Mixed C++/CLI code with Berkeley DB

Mixed C++/CLI code with Berkeley DB

Scheduled Pinned Locked Moved Managed C++/CLI
databasec++dotnetdata-structures
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    anti AS
    wrote on last edited by
    #1

    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!

    J 1 Reply Last reply
    0
    • A anti AS

      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!

      J Offline
      J Offline
      jschell
      wrote on last edited by
      #2

      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.

      A 1 Reply Last reply
      0
      • J jschell

        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.

        A Offline
        A Offline
        anti AS
        wrote on last edited by
        #3

        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);
        
        J 1 Reply Last reply
        0
        • A anti AS

          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);
          
          J Offline
          J Offline
          jschell
          wrote on last edited by
          #4

          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.)

          A 1 Reply Last reply
          0
          • J jschell

            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.)

            A Offline
            A Offline
            anti AS
            wrote on last edited by
            #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");

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups