There is a table:

CREATE TABLE HOOKNAMES ( HN_ID SMALLINT NOT NULL PRIMARY KEY, HN_NAME VARCHAR(20) NOT NULL, HN_LEVEL SMALLINT NOT NULL, HN_IMAGE BLOB); 

The first 3 fields are filled with this:

 Form1.IBSql1.SQL.Clear; Form1.IBSql1.SQL.Append('INSERT INTO HOOKNAMES (HN_ID, HN_NAME, HN_LEVEL) '); Form1.IBSql1.SQL.Append('VALUES ('+TempBuf[3]+', '+TempBuf[0]+', '+TempBuf[1]+')'); Form1.IBSql1.ExecQuery; 

I try to add the last field as follows (I found an example on this site):

 Form1.HookNamesDS.Edit; TBlobField(Form1.HookNamesDS.FieldByName('HN_IMAGE')).LoadFromFile('..\bilder\image.png'); Form1.HookNamesDS.Post; 

Var HookNamesDS: TIBDataSet;

I get an error when I try to change the DataSet, which is read-only, the DataSource is not specified in it, the entire table is simply selected via SelectSQL.
How in this example is the binding exactly to the desired line?
How to properly initialize the DataSet? And how to work with him?

  • @Yura Ivanov, here it is! :) I thought DataSet is screwed just because otherwise there is nothing to enter into the BLOB field, and everything ingenious, as usual, is simple) IBSql also knows how. Yes, I just needed to create and populate the database - Isaev

1 answer 1

You first need to ensure that the table is modifiable. If you use TIBDataSet , then you need to register all sql queries: SelectSQL , ModifySQL , DeleteSQL , InsertSQL . If you add a component to a form, you can set these requests in the visual editor (PCM on the component, Dataset editor ...), select the name of the table in it, requests will be created, afair yourself. If you create a component in runtime, then you need to write the same requests in the code. About the texts of requests wrote here .

After you get a modifiable dataset, you can now start saving blobs.

UPD Concerning TIBSQL. Never write requests like this. Always use the options.

UPD2 If you add or edit a non-existing record (for example, the table is empty - Insert will be called), in this case you need to fill in the HN_ID . For a change, as in the question, HookNamesDS.Edit - HN_ID you for the current record must be already filled. In any case, you need to fill in the field, not the parameter. The parameters will be filled by themselves, this component deals with.

 with Form1.HookNamesDS do begin Insert; FieldByName('HN_ID').AsString:=TempBuf[3]; // остальные поля TBlobField(FieldByName('HN_IMAGE')).LoadFromFile('..\bilder\image.png'); Post; end; 

And so, your code from the question should work.

  • Thanks, requests registered, the error was gone. SQL queries were registered in principle in the same way as you described the link above, deleted too much. And now how to correctly load the image into the database and link it with the BLOB field? And how to pass the parameters? When trying TBlobField (Form1.HookNamesDS.FieldByName ('HN_IMAGE')). LoadFromFile ('.. \ bilder \ image.png'); Error, "HN_ID parameter must be specified". At attempt of Form1.HookNamesDS.ParamByName ('HN_ID'). AsString: = TempBuf [3]; "Field 'HN_ID' not found". - Isaev
  • updated the answer - Yura Ivanov
  • Thank you so much, it works now! - Isaev
  • Is it possible to use something else as a DataSet instead of this component? Somehow he seems far-fetched with his unnecessary scripts for each operation ... Or maybe you can do without it when filling in the BLOB field? Everything worked without them, but because of one field, one has to fence - Isaev
  • one
    @Isaev, yes, if you just need to perform insertion queries, you can use TIBSQL (use parameters with this component): with Form1.IBSql1 do begin SQL.Text: = 'INSERT INTO HOOKNAMES (HN_ID, HN_IMAGE)' + ' VALUES (: HN_ID,: HN_IMAGE) '; ParamByName ('HN_ID'). AsString: = TempBuf [3]; ParamByName ('HN_IMAGE'). LoadFromFile ('...'); ExecQuery; end; In general, it all depends on your needs. It is quite natural that one should use what is simpler and more suitable for each individual case. - Yura Ivanov