Closed Al-Muhandis closed 6 years ago
So, I've done so far is an interim solution, work directly with the MySQL function
opfEntity.Add(AnEntity);
opfEntity.Apply;
dSQLdbQuery.SQL.Text:='SELECT LAST_INSERT_ID() AS ID';
dSQLdbQuery.Open;
AnEntity.id:=dSQLdbQuery.Field('ID').AsInteger;
write('AnEntity ID is '+IntToStr(AnEntity.id)); // Now there are valid last IndexID!!
But I don't think that's the best solution. Surely this should be implemented by means of the dopf itself. If anyone knows prompt, I will alter the code to make it more beautiful
Hello @Al-Muhandis . A possible improvement would be in the broker connection, something like GetNextValueSQL
of the TSQLConnection
, but using a better name like NextID
or NewID
. I'm busy in the B4 development in my free time, but if you provide a patch it could be checked and applied. :-)
Hello @silvioprog ! I think it is better to using function with name like the LastInsertID
). This is quite transparent and understandable for those who know [my]SQL.
The fact is that the LastInsertID
(as I see) is in the implementation for MySQL 5.5 (at least) in the tconnectionname
and TCursorName
classes which are used in dopf. See the mysql55conn
unit.
That's why I thought it was possible to get this value using dopf functional instead of the direct method I used in the code example above. My code from the fact that this is direct and is not suitable for all due to different nuances. For example, the field id
should be AUTOINC (how does dopf know about this?) and some(?) others. Therefore, in this case, my knowledge is not enough to write universal code that would not cause incomprehensible results in other cases.
This is not a big problem I think for those who know SQL, just thought there is a possibility to isolate the code from MySQL in this case. In any case, thanks for the answer!
Closing due to already solved in the application. Please feel free to reissue or send it as new feature! :smiley:
How to get LastInsertID in dopf lib?
Connection driver 'MySQL 5.5'