Skip to content

MySQL Module ConnectionClass execute

DizzasTeR edited this page Jan 30, 2021 · 5 revisions

MySQL Connection

execute

Executes a mysql query and returns the number of affected rows

int MySQLConnection:execute([function callback, ] string query [, ...optionalParameters])

Parameters

  • optional string function — Function to call with the number of affected rows (OPTIONAL)
  • string query — The query to execute
  • optional optionalParameters — Forward any extra parameters with the query (Used for prepared statements)

Returns

int - Number of affected rows if successful, false if the query failed

Example #1

local result = connection:execute("UPDATE test_table SET name = ? WHERE id = ?", {"dizzo", 1})
if result ~= false then
    print("Affected rows execution result 1: "..tostring(result))

    result = connection:execute("UPDATE test_table SET name = ? WHERE id = ?", {[2] = 2, [1] = "epic"})
    print("Affected rows execution result 2: "..tostring(result))

    -- Simple statement (Works for query and insert as well)
    result = connection:execute("UPDATE test_table SET name = 'topkek' WHERE id = 2")
    print("Affected rows execution result 3: "..tostring(result))
end

Example #2

-- callback versions
connection:execute(function(result)
    print("OK")
end, "UPDATE test_table SET score = 100 WHERE name LIKE ?", {"%dizz%"})
Clone this wiki locally