Documentation Home
X DevAPI User Guide
Download this Manual

X DevAPI User Guide  /  Working with Result Sets  /  Working with AUTO-INCREMENT Values

Pre-General Availability Draft: 2017-09-18

9.3 Working with AUTO-INCREMENT Values

A common MySQL task is to use AUTO_INCREMENT columns, for example generating primary key values. 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 following methods to return AUTO_INCREMENT column values from the return value of table.insert():

  • getFirstAutoIncrementValue()

  • getAutoIncrementValues()

In the following examples it is assumed that the table contains a column for which the AUTO_INCREMENT attribute is set. Furthermore it is assumed that all insertions succeed. The getFirstAutoIncrementValue() function is used when adding rows individually, or in other words when not chaining table.insert() calls. For example:

res = tab.insert(['name']).values('Sakila'}.execute();
print(res.getFirstAutoIncrementValue());

When you chain multiple table.insert() calls, there are potentially multiple AUTO_INCREMENT values returned. The getAutoIncrementValues() function returns a list of all AUTO_INCREMENT values generated when inserting multiple rows:

res = tab.insert(['name']).values('Sakila').values('Otto').execute();
print(res.getAutoIncrementValues());

// prints a list of values for 'Sakila' and 'Otto'

Note that AUTO_INCREMENT columns may be used for generating primary key or id values but are not limited to them.


User Comments
Sign Up Login You must be logged in to post a comment.