Page 1 of 1

How to insert/update pictures in Firebird

Posted: 20.10.2011, 20:08
by christensen
Hi,

How can i insert pictures in database (Firebird 2.1.3) blob fields?

The field in my db are:
id_test(Bigint)
name_test(Varchar)
img_test(Blob, 80, Binary)

I can insert the picture but i want to do at once:(id_test is a autoincrement field) 'name_test' and the 'img_test'.

For the picture:

Code: Select all

with ZQuery1 do
  begin
    Insert;
     TBlobField( fieldByName('img_test')).LoadFromFile('C:\pics.jpg');
    Post;
  end;
EDIT LATER:
I found the solution:

Code: Select all

   FieldByName('name_test').AsString:=Edit1.Text;  
But now i have a problem to edit the pictures? how can i update the blob field?

My system: Lazarus 0.9.30, Zeoslib 6.6,Firebird 2.13

Posted: 20.10.2011, 21:37
by seawolf
The following example is taken from test\dbc\ZTestDbcInterbase.pas

procedure TZTestDbcInterbaseCase.TestBlobs;
var
Connection: IZConnection;
PreparedStatement: IZPreparedStatement;
Statement: IZStatement;
ResultSet: IZResultSet;
TextStream: TStream;
ImageStream: TMemoryStream;
TempStream: TStream;
begin
Connection := CreateDbcConnection;
Statement := Connection.CreateStatement;
CheckNotNull(Statement);
Statement.SetResultSetType(rtScrollInsensitive);
Statement.SetResultSetConcurrency(rcReadOnly);

Statement.ExecuteUpdate('DELETE FROM BLOB_VALUES WHERE B_ID='
+ IntToStr(TEST_ROW_ID));

TextStream := TStringStream.Create('ABCDEFG');
ImageStream := TMemoryStream.Create;
ImageStream.LoadFromFile('../../../database/images/zapotec.bmp');

PreparedStatement := Connection.PrepareStatement(
'INSERT INTO BLOB_VALUES (B_ID, B_TEXT, B_IMAGE) VALUES(?,?,?)');
PreparedStatement.SetInt(1, TEST_ROW_ID);
PreparedStatement.SetAsciiStream(2, TextStream);
PreparedStatement.SetBinaryStream(3, ImageStream);
CheckEquals(1, PreparedStatement.ExecuteUpdatePrepared);

ResultSet := Statement.ExecuteQuery('SELECT * FROM BLOB_VALUES'
+ ' WHERE b_id=' + IntToStr(TEST_ROW_ID));
CheckNotNull(ResultSet);
Check(ResultSet.Next);
CheckEquals(TEST_ROW_ID, ResultSet.GetIntByName('B_ID'));
TempStream := ResultSet.GetAsciiStreamByName('B_TEXT');
CheckEquals(TextStream, TempStream);
TempStream.Free;
TempStream := ResultSet.GetBinaryStreamByName('B_IMAGE');
CheckEquals(ImageStream, TempStream);
TempStream.Free;
ResultSet.Close;

TextStream.Free;
ImageStream.Free;

Statement.Close;
end;