DbPoll: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
Line 16: Line 16:
*''false'' Returns false if the query string contained an error, the connection has been lost or the query handle is incorrect. This automatically frees the query handle, so you do not have to call [[dbFree]].
*''false'' Returns false if the query string contained an error, the connection has been lost or the query handle is incorrect. This automatically frees the query handle, so you do not have to call [[dbFree]].
** This also returns two extra values: error code and error message. See the example on how to retrieve them,
** This also returns two extra values: error code and error message. See the example on how to retrieve them,
*''table:'' Returns a table when the results are ready. This automatically frees the query handle, so you do not have to call [[dbFree]].
*''table:'' Returns a table when the query has successfully completed. This automatically frees the query handle, so you do not have to call [[dbFree]].
** This also returns an extra value: number of affected rows
** This also returns an extra value: number of affected rows


Line 27: Line 27:
This example shows the possible return values:
This example shows the possible return values:
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
local result, numrows, errmsg = dbPoll ( qh, -1 )
local result, num_affected_rows, errmsg = dbPoll ( qh, -1 )


if result == nil then
if result == nil then
     outputConsole( "dbPoll result not ready yet" )
     outputConsole( "dbPoll result not ready yet" )
elseif result == false then
elseif result == false then
     outputConsole( "dbPoll failed. Error code: " .. tostring(numrows) .. "  Error message: " .. tostring(errmsg) )
     outputConsole( "dbPoll failed. Error code: " .. tostring(num_affected_rows) .. "  Error message: " .. tostring(errmsg) )
else
else
     outputConsole( "dbPoll succeeded. Number of affected rows: " .. tostring(numrows) )
     outputConsole( "dbPoll succeeded. Number of affected rows: " .. tostring(num_affected_rows) )
end
end
</syntaxhighlight>
</syntaxhighlight>
Line 40: Line 40:
This example shows how to handle the result if the query selected data:
This example shows how to handle the result if the query selected data:
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
local result, numrows, errmsg = dbPoll ( qh, -1 )
local result, num_affected_rows, errmsg = dbPoll ( qh, -1 )


if numrows > 0 then
if num_affected_rows > 0 then
      
      
for result, row in pairs ( result ) do
for result, row in pairs ( result ) do

Revision as of 19:28, 3 September 2012

This function checks the progress of a database query.

Syntax

table dbPoll ( handle queryHandle, int timeout )

Required Arguments

  • queryHandle: A query handle previously returned from dbQuery
  • timeout: How many milliseconds to wait for a result. Use 0 for and instant response (which may return nil). Use -1 to wait until a result is ready. Note: A wait here will freeze the entire server just like the executeSQL* functions

Returns

  • nil: Returns nil if the query results are not yet ready. You should try again in a little while. (If you give up waiting for a result, be sure to call dbFree)
  • false Returns false if the query string contained an error, the connection has been lost or the query handle is incorrect. This automatically frees the query handle, so you do not have to call dbFree.
    • This also returns two extra values: error code and error message. See the example on how to retrieve them,
  • table: Returns a table when the query has successfully completed. This automatically frees the query handle, so you do not have to call dbFree.
    • This also returns an extra value: number of affected rows

Example

This example waits until a result is ready:

local result = dbPoll ( qh, -1 )

This example shows the possible return values:

local result, num_affected_rows, errmsg = dbPoll ( qh, -1 )

if result == nil then
    outputConsole( "dbPoll result not ready yet" )
elseif result == false then
    outputConsole( "dbPoll failed. Error code: " .. tostring(num_affected_rows) .. "  Error message: " .. tostring(errmsg) )
else
    outputConsole( "dbPoll succeeded. Number of affected rows: " .. tostring(num_affected_rows) )
end

This example shows how to handle the result if the query selected data:

local result, num_affected_rows, errmsg = dbPoll ( qh, -1 )

if num_affected_rows > 0 then
    
	for result, row in pairs ( result ) do
		-- by using a second loop (use it if you want to get the values of all columns the query selected):
		for column, value in pairs ( row ) do
			-- column = the mysql column of the table in the query
			-- value = the value of that column in this certain row
		end
		
		-- or without a second loop (use it if you want to handle every value in a special way):
		outputChatBox ( row["column"] ) -- it will output the value of the column "column" in this certain row
	end

end

Requirements

This template will be deleted.

See Also