Skip to content

MySQL Module ConnectionClass execute

DizzasTeR edited this page Nov 2, 2020 · 5 revisions

MySQL Connection

execute

Executes a mysql query and returns the number of affected rows

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

Parameters

  • 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:query(function(result)
    print("OK")
end, "SELECT * FROM test_table WHERE name LIKE ?", {"%dizz%"})

Clone this wiki locally