AUTO_INCREMENT
columns can be used in MySQL for
generating primary key or id
values, but are
not limited to these uses. This section explains how to retrieve
AUTO_INCREMENT
values when adding rows using
X DevAPI. For more background information, see
Using AUTO_INCREMENT.
X DevAPI provides the getAutoIncrementValue()
method to return the first AUTO_INCREMENT
column value that was successfully inserted by the operation,
taken from the return value of table.insert()
.
In the following example it is assumed that the table contains a
PRIMARY KEY
column for which the
AUTO_INCREMENT
attribute is set:
res = myTable.insert(['name']).values('Mats').values('Otto').execute();
print(res.getAutoIncrementValue());
This table.insert()
operation inserted multiple
rows. getAutoIncrementValue()
returns the
AUTO_INCREMENT
column value generated for the
first inserted row only, so in this example, for the row
containing “Mats”. The reason for this is to make it
possible to reproduce easily the same operation against some other
server.