2011-07-12 23 views
0

こんにちは、バイナリデータを保存する必要のあるiphoneアプリケーションを作成しています。 Ultraliteデータベースの画像。 私はこの目的のために次のコードを使用しています。Ultraliteデータベースのバイナリデータを挿入中にエラーが発生しました。

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"file_name" ofType:@"png"]; 
NSData *data = [NSData dataWithContentsOfFile:filePath]; 
NSUInteger len = [data length]; 
ul_binary *byteData = (ul_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(); 
} 

このコードでは、行にエラー 'EXC_BAD_ERROR' を与えている::

table->SetBinary(3, byteData); 

を私は、この行をコメントする場合、このコードは正常に動作します。

助けていただけたら幸いです! おかげ

答えて

0

ul_binaryの定義はこれです:

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; 

だからそれは構造体です。 memcpyを実行するだけで、lenフィールドとすべてのものが上書きされます。だからここに(私の知る限り)あなたがそれを行う必要があります方法は次のとおりです。

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

また、あなたがメモリを割り当てることを試みる前に、そのlen <= MAX_UL_BINARYをチェックする必要があります。そして、free(byteData);を忘れないでください。

+0

ありがとうございます。それは今働く。とても役に立ちました。 –

関連する問題