Visualizzazione dei risultati da 1 a 2 su 2
  1. #1
    Utente di HTML.it L'avatar di NA01
    Registrato dal
    May 2004
    Messaggi
    113

    [berkeley db] i record che scappano

    vi pongo un problemuccio...
    ho un database (berkeley db) in un simpatico programma in c++.
    inserisco un record in questo db e provo a riprenderlo con la stessa chiave.
    risultato....

    record not found VVoVe:

    ovviamente sbaglio io qualcosa, ma non so cosa
    lo ho sempre usato in c, e nell'imparare a usare le librerie da quelle per c a quelle per c++ magari ho fatto una pioggia di casini...

    vi propongo una versione estratta da vari punti delle classi del codice incriminato:

    grazie, ciao!

    codice:
    #include <iostream>
    #include <string>
    #include <db_cxx.h>
    #include "Projects/NA++/Entry.h"
    #include "Projects/NA++/Exceptions.h"
    
    void insertRecord(Entry record,std::string insertKey, Db* db){
      Dbt key(const_cast<char*>(insertKey.c_str()), insertKey.length() + 1);
      Dbt data(&record, sizeof(Entry));
      db->put(NULL, &key, &data, 0);
    }
    
    Entry* getRecord(std::string getKey, Db* db){
      Dbt key, data;
      key.set_data(const_cast<char*>(getKey.c_str()));
      key.set_ulen(getKey.length() + 1);
      key.set_flags(DB_DBT_USERMEM);
      if(db->get(NULL, &key, &data, 0) == DB_NOTFOUND){
        throw NotFound();
      }
      return (Entry*)data.get_data();
    }
    
    
    int main(){
      try{
        Db* db;
        db = new Db(NULL, 0);
        u_int32_t oFlags = DB_CREATE;
        db->open(NULL, "db.db", NULL, DB_BTREE, oFlags, 0);
        std::string key1 = "key1", key2 = "key2";
        Auth status = DENIED;
        Entry entry1, entry2;
        strncpy(entry1.hash, "abcdefasjkjnklke", 17);
        strncpy(entry2.hash, "lkjhgfdsaqwertyu", 17);
        entry1.status = entry2.status = status;
        insertRecord(entry1, key1,db);
        insertRecord(entry2,key2,db);
        Entry* entry3 = getRecord(key1,db);
        Entry* entry4 = getRecord(key2,db);
      }catch(NotFound e){
        std::cout << "Ma pork!!!\n";
      }
    }

  2. #2
    Utente di HTML.it L'avatar di NA01
    Registrato dal
    May 2004
    Messaggi
    113

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.