Lua DBQuery

From ArcEmu-Wiki
Jump to: navigation, search


Lua Database Commands

WorldDBQuery() and CharDBQuery()

Performs a query on the world database or on the char database. Returns a "QueryResult" object.

pResult = WorldDBQuery(string sql)

pResult = CharDBQuery(string sql)

This command can be used for SQL queries like SELECT, INSERT, UPDATE, DELETE etc.
See MySQL Reference Manual for details about SQL Syntax.


Returns the column at the specified place (starting from 0 for the first column). Will return an error if you specify a number greater than the amount of columns in the selected table.


pCol = pResult:GetColumn(int column_index)

Using :Get<DataType>() after a :GetColumn(number) will return the value in the column as the specified data type.

Example data types



Get number of rows in the query result


int rows = pResult:GetRowCount()


Get number of columns in the query result


int cols = pResult:GetColumnCount()


Fetches the next row of the SQL query. Returns false if no more row exist.


bool = pResult:NextRow()

Example 1

  local sql = "SELECT * FROM items WHERE entry = 29434"
  local result = WorldDBQuery(sql)
  print(result:GetColumn(4):GetString())  -- Output: Badge of Justice.

Example 2

function SQLTest( event, player, message, type, language )
  if( message == "#sql" ) then
    local result = WorldDBQuery( "SELECT `entry`, `name` FROM `creature_names` LIMIT 5" );
    if( result ~= NIL ) then
      local rowcount = result:GetRowCount();
      local colcount = result:GetColumnCount();
      print( "rows:" .. rowcount .. "  columns:" .. colcount );
        for col = 0, colcount-1, 1 do
          io.write( result:GetColumn( col ):GetString() .. "  " );
      until result:NextRow() ~= true;
RegisterServerHook( 16, "SQLTest" );   -- Register OnChatMessage
Personal tools