Changes

Jump to navigation Jump to search
499 bytes added ,  18:38, 1 December 2023
m
Text replacement - "syntaxhighlight" to "pre"
<p>In the The query mode of the [[Clients]] allows you to bind external variables to a query can be send to and evaluate the server and executedquery in an iterative manner. For this you have to call the The <code>query()</code> functionof the <code>Session</code> with your defined query. This will return instance returns a new query objectwhich 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 <code>bind()</code> functionof the query objectinstance.</p>
==Usage==
<p>The query execution works as follows:</p>
#Create a new session instance with hostname, port, username and password.#Call the <code>query()</code> function of the session with the query as argument your XQuery expression to get your a query object.#Optionally bind variables to the query with one of the <code>bind()</code> functionfunctions.#Initialize query output Optionally bind a value to the context item via <code>initcontext()</code>.#Iterate through the query object with the <code>more()</code> and <code>next()</code> functions. If # As an error occursalternative, an exception is throwncall <code>execute()</code> to get the whole result at a time.# <code>info()</code> gives you information on query evaluation.# <code>options()</code> returns the query serialization parameters.#Close Don't forget to close the query with <code>close()</code>.
==Python PHP Example==  Taken from our [https://github.com/BaseXdb/basex/blob/master/basex-api/src/main/php/QueryBindExample.php repository]: <pre lang="php"><?php/* * This example shows how queries can be executed in an iterative manner. * Documentation: http://basex.org/api * * (C) BaseX Team 2005-15, BSD License */include("BaseXClient.php");
<pre classtry { // create session $session =new Session("localhost", 1984, "brush:pythonadmin", "..."); try { // create query instance $input = 'declare variable $name external; '. 'for $i in 1 to 10 return element { $name } { $i }'; $query = $session->import BaseXClient, timequery($input);
try: // bind variable # initialize timer start = time.clock $query->bind("$name", "number");
# create session // print result session = BaseXClient.Session print $query->execute('localhost', 1984, 'admin', 'admin')."\n";
# perform command and print returned string // close query instance print session.execute $query->close("xquery 1 to 10");
# close session} catch (Exception $e) { // print exception session.close print $e->getMessage(); }
# print time needed// close session time = $session->close(time.clock() - start) * 1000 print time, "ms.";
except IOError as } catch (Exception $e:) { # // print exception print $e->getMessage();}?>
</pre>
[[Category:Developer]][[Category:Server]]=Changelog= ;Version 7.2 [[Category* Added:Language Bindings]]{{Code|context()}} function
Bureaucrats, editor, reviewer, Administrators
13,554

edits

Navigation menu