5.11. Example - Asynchronous Queries

This example uses the asynchronous query commands described in Section 4.4, “Asynchronous Query Processing Commands”. It shows how a query could be sent to the database, and the application could continue processing without blocking while waiting for the results to come back. It also shows part of how a Cancel button might be implemented. Note: this is not a complete script.

Example 5.18. Asynchronous Queries

pg_sendquery $conn "SELECT * FROM tables... WHERE conditions ORDER BY ..."
# Wait for not-busy condition:
while {[pg_isbusy $conn]} {
   ... Do something here, like check for user input and update the interface...
   ... Good idea to insert a delay so it doesn't busy-wait ...
   ... Check for cancel button ...
   if {$user_pressed_cancel_button} break
}

# Did the user hit the Cancel button? If so, try to cancel the query.
if {$user_pressed_cancel_button} {
  pg_cancelrequest $conn
}

# Fetch results until it there are none left (even if canceled):
while {[set res [pg_getresult $conn]] != ""} {
  if {!$user_pressed_cancel_button} {
    ... do something with the query result $res ...
  }
  pg_result $res -clear
}


SourceForge.net Logo

This version of the manual was produced for the Pgtcl-ng Sourceforge project web service site, which requires the logo on each page.

To download a logo-free copy of the manual, see the Pgtcl-ng project downloads area.