self:update

Server

Appends a row update to the query


Syntax

local query = self:update(
    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.

  • Chaining — compatible with :where to target specific rows for updating
  • Multiple updates — chain multiple :update calls to append additional column updates
  • Multiple conditions — chain multiple :where calls to apply additional filters to the update
  • Without conditions — omitting :where will update all rows in the table

Parameters

TypeNameDescription
tabledataA key-value table defining the columns to update
• Keys represent column names
• Values represent the new data to apply — accepted types are string and number

Returns

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

Examples

Update a column for a specific row in the 'players' table
local db = database.create("127.0.0.1", "root", "", "vital_sandbox")

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

    if not ok then
        engine.print("error", err)
    end
end):resume()
Update all rows in the 'players' table without a condition
local db = database.create("127.0.0.1", "root", "", "vital_sandbox")

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

    if not ok then
        engine.print("error", err)
    end
end):resume()
Chain multiple updates and multiple conditions 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")
        :update({ score = 1000 })
        :update({ rank = "elite" })
        :where("name", "=", "aviril")
        :where("score", ">=", 500)
        :execute())

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

On this page