9951 explained code solutions for 126 technologies


lua-mysqlHow to get last inserted ID


mysql = require "luasql.mysql"
local pool = mysql.mysql()
local db = pool:connect('test', 'usr', 'pwd')

db:execute('INSERT INTO test SET name = "Joe"')
local id = db:getlastautoid()ctrl + c
require "luasql.mysql"

includes mysql module to work with database

mysql.mysql()

init Mysql connection

pool:connect

connect to Mysql server based on specified credentials

db:execute

executes given query and returns cursor to iterate through results

:getlastautoid()

returns last inserted ID for current connection

local id

returned ID


Usage example

mysql = require "luasql.mysql"
local pool = mysql.mysql()
local db = pool:connect('test', 'usr', 'pwd')

db:execute('INSERT INTO test SET name = "Joe"')
local id = db:getlastautoid()

print(id)
output
19