self:insert

Server

Appends a row insertion to the query


Syntax

local query = self:insert(
    data
)

This function only prepares the query and does not execute it. Chain db_query:execute at the end to commit the query to the database.

  • Multiple insertions — chain multiple :insert calls to append additional row insertions

Parameters

TypeNameDescription
tabledataA key-value table defining the row to insert
• Keys represent column names
• Values represent the data to insert — accepted types are string and number

Returns

TypeNameDescription
db_query | boolqueryQuery builder instance on successful execution, or false on failure

Examples

Insert a new player record
local db = database.create("127.0.0.1", "root", "", "vital_sandbox")

thread.create(function(self)
    local ok, err = self:await(db:table("players")
        :insert({ name = "aviril", score = 0 })
        :execute())

    if not ok then
        engine.print("error", err)
    end
end):resume()
Chain multiple row insertions in a single execution
local db = database.create("127.0.0.1", "root", "", "vital_sandbox")

thread.create(function(self)
    local ok, err = self:await(db:table("players")
        :insert({ name = "aviril", score = 0 })
        :insert({ name = "xaero", score = 100 })
        :execute())

    if not ok then
        engine.print("error", err)
    end
end):resume()

On this page