lastrowid

Overview

Returns the automatically generated ID value from an INSERT statement on a table that has an AUTO_INCREMENT column.

See also: MariaDB Connector/Python 1.1 and in 1.0

EXAMPLES

Given the following "sample" table in database "test":

CREATE TABLE sample (
  id BIGINT AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(64)
);

The following will INSERT 3 rows and print the auto-assigned id of each one:

import mariadb

conn = mariadb.connect(
    host = 'localhost',
    user = 'root',
    password = 'secret',
    database = 'test',
    )

cursor = conn.cursor()

for name in ('foo', 'bar', 'baz'):
    cursor.execute("INSERT INTO sample SET name = ?", (name,))
    print(cursor.lastrowid, name)

conn.close()

The output when starting with an empty table would look like this:

1 foo
2 bar
3 baz

CHANGE HISTORY

Release Series

History

1.1

  • Present starting in MariaDB Connector/Python 1.1.2.

1.0

  • Present starting in MariaDB Connector/Python 1.0.0.