Difference between revisions of "Query Mode"

From BaseX Documentation
Jump to navigation Jump to search
Line 19: Line 19:
 
==Example in [https://svn.uni-konstanz.de/dbis/basex/trunk/api/etc/php/ PHP]==  
 
==Example in [https://svn.uni-konstanz.de/dbis/basex/trunk/api/etc/php/ PHP]==  
  
−
<pre class="brush:python">
+
<pre class="brush:php">
−
import BaseXClient, time
+
<?php
 +
/*
 +
* This example shows how database commands can be executed.
 +
* Documentation: http://basex.org/api
 +
*
 +
* (C) BaseX Team 2005-11, ISC License
 +
*/
 +
include("BaseXClient.php");
  
−
try:
+
try {
−
   # initialize timer
+
   // initialize timer
−
   start = time.clock()
+
   $start = microtime(true);
  
−
   # create session
+
   // create session
−
   session = BaseXClient.Session('localhost', 1984, 'admin', 'admin')
+
   $session = new Session("localhost", 1984, "admin", "admin");
 +
 
 +
  // perform command and print returned string
 +
  print $session->execute("xquery 1 to 10");
  
−
   # perform command and print returned string
+
   // close session
−
   print session.execute("xquery 1 to 10")
+
   $session->close();
  
−
   # close session
+
   // print time needed
−
   session.close()
+
   $time = (microtime(true) - $start) * 1000;
 +
  print "\n$time ms\n";
  
−
  # print time needed
+
} catch (Exception $e) {
−
  time = (time.clock() - start) * 1000
+
   // print exception
−
   print time, "ms."
+
   print $e->getMessage();
−
 
+
}
−
except IOError as e:
+
?>
−
  # print exception
 
−
   print e
 
 
</pre>
 
</pre>
  

Revision as of 13:27, 10 January 2011

In the query mode a query can be send to the server and executed in an iterative manner. For this you have to call the query() function of the Session with your defined query. This will return a query object which comes with an forward-only iterator to get each result of the query. Furthermore it is possible to bind variables to the query using the bind() function of the query object.

Usage

The query execution works as follows:

  1. Create a new session instance with hostname, port, username and password.
  2. Call the query() function of the session with the query as argument to get your query object.
  3. Optionally bind variables to the query with the bind() function.
  4. Initialize query output via init().
  5. Iterate through the query object with the more() and next() functions. If an error occurs, an exception is thrown.
  6. Close the query with close().

Example in PHP

<?php
/*
 * This example shows how database commands can be executed.
 * Documentation: http://basex.org/api
 *
 * (C) BaseX Team 2005-11, ISC License
 */
include("BaseXClient.php");

try {
  // initialize timer
  $start = microtime(true);

  // create session
  $session = new Session("localhost", 1984, "admin", "admin");
  
  // perform command and print returned string
  print $session->execute("xquery 1 to 10");

  // close session
  $session->close();

  // print time needed
  $time = (microtime(true) - $start) * 1000;
  print "\n$time ms\n";

} catch (Exception $e) {
  // print exception
  print $e->getMessage();
}
?>

for more examples see the languages section.