NDB API and blobs: some example

I spend some time on this last night to figure it out with MySQL Cluster NDB API.. Actually, I had to ask my dear colleagues for help. Pekka gave a tip, I added some weird logic: bingo!

Actually, nothing special, but here is the silly code inserting a record into a table with a BLOB field. The oper->equal("id",4) line and the order of things are important. Note: it is missing lots of stuff, like checking for errors!

Enjoy! I might blog more of those silly things later on. Hopefully it makes the manual. Jon, this one is or you!

void blob_insert(Ndb *ndb)
{
         const NdbDictionary::Dictionary* dict = ndb->getDictionary();
         const NdbDictionary::Table *table = dict->getTable("blobby");

         NdbTransaction *trans = ndb->startTransaction();
         NdbOperation *oper = trans->getNdbOperation(table);

         // The data we inserting in the blob field
         char data[60] = "Blobby is a good dog.";

         // We insert a new record
         oper->insertTuple();

         // Even though the record with id X doesn't exists...
         oper->equal("id",4);

         // Get a blob handle
         NdbBlob *blob = oper->getBlobHandle("blobby");

         // Put the data in the blob handle
         blob->setValue((void*)&data,strlen(data));

         // And commit it
         trans->execute( NdbTransaction::Commit );
         trans->close();
}

Goodluck! Ha! I’m going zzz..