开发者

Error during insert binary data in Ultralite database iPhone

开发者 https://www.devze.com 2023-03-19 10:15 出处:网络
Hi I am writing a iphone application where I need to store binary data i.e.; image in the Ultralite database.

Hi I am writing a iphone application where I need to store binary data i.e.; image in the Ultralite database. I am using following code for this purpose.

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"file_name" ofType:@"png"];
NSData *data = [NSData dataWithContentsOfFile:filePath];
NSUInteger len = [data length];
ul_binary *byteData = (u开发者_开发技巧l_binary*)malloc(len);
memcpy(byteData, [data bytes], len);

ULTable *table = connection->OpenTable("NAMES");
if(table->InsertBegin()){
    table->SetInt(1, (maxId+1));
    table->SetString(2, [name UTF8String]);
    table->SetBinary(3, byteData);
    table->Insert();
    table->Close();
    connection->Commit();
}

This code is giving error 'EXC_BAD_ERROR' on line::

table->SetBinary(3, byteData);

This code works fine if i comment this line.

Any help would be appreciated! Thanks


The definition of ul_binary is this:

typedef struct ul_binary {
    /// The number of bytes in the value.
    ul_length       len;
    /// The actual data to be set (for insert) or that was fetched (for select).
    ul_byte     data[ MAX_UL_BINARY ];
} ul_binary, * p_ul_binary;

So it's a struct. By simply doing the memcpy as you do, you also overwrite the len field and everythings messed up. So here's how you should do it (as far as I can see):

ul_binary *byteData = (ul_binary *)malloc(sizeof(ul_binary));
memcpy(&byteData->data, [data bytes], len);
byteData->len = len;

You also need to check that len <= MAX_UL_BINARY before you try to allocate the memory. And don't forget to free(byteData);.

0

精彩评论

暂无评论...
验证码 换一张
取 消