[fpc-pascal] How to insert a record and get the primary key with sqldb?

Michael Van Canneyt michael at freepascal.org
Sat Oct 22 16:01:40 CEST 2011

On Sat, 22 Oct 2011, Martin Schreiber wrote:

> On Saturday 22 October 2011 15.21:40 Marco van de Voort wrote:
>> In our previous episode, Felipe Monteiro de Carvalho said:
>>> Ok, now I want to insert a record in my table and I would like to
>>> obtain the auto-generated PrimaryKey
>> This is a classic problem, since SQL simply doesn't support this.
>> So all DBs do something else, for postgresql there are sequence objects
>> that can be queried, while other allow to return the id of the
>> autogenerated fields.
>> Afaik sqldb does not abstract this yet.
> MSEgui updates inserted primary key fields automatically for MySQL and SQLite3
> by "lastinsertid". IIRC sqldb does the same?

Not to my knowledge. 
It stores the lastinsertid in the cursor, but this is not made available to the programmer.

> For database which support the "returning" clause tmse*field.providerflags1
> pf1_refreshinsert and pf1_refreshupdate can be used, this works for all
> fields.

Hardly an abstraction, it's just some automatisms :-)

But good to know. 
We should really overhaul sqldb and add some of the MSEgui features.

Thanks, Martin.


More information about the fpc-pascal mailing list