Difference between revisions of "Query Mode"

From BaseX Documentation
Jump to navigation Jump to search
Line 19: Line 19:
 
==Example==  
 
==Example==  
 
   
 
   
#Create a session object: <code>Session session = new Session("localhost", 1984, "admin", "admin");</code>
+
 
#Define a query and create a query object:<br/><pre class="brush:java">String q = "declare variable $name external; for $i in 1 to 10 return element { $name } { $i }"; &#10;Query query = session.query(q);</pre>
+
<pre class="brush:java">
#Call the bind method of your query object:<br/><pre class="brush:java">query.bind("$name", "Number");</pre>
+
// Create a session object
#Call the init method of your query object:<br/><pre class="brush:java">print query.init();</pre>
+
Session session = new Session("localhost", 1984, "admin", "admin");
#Iterate through the query object:<br/><pre class="brush:java">while(query.more()) print query.next();</pre>
+
 
#Close your query object:<br/><pre class="brush:java">print query.close();</pre>
+
//Define a query and create a query object
 +
String q = "declare variable $name external; for $i in 1 to 10 return element { $name } { $i }"
 +
Query query = session.query(q);
 +
 
 +
//Call the bind method of your query object
 +
query.bind("$name", "Number");
 +
 
 +
//Call the init method of your query object
 +
print query.init();
 +
 
 +
//Iterate through the query object
 +
while(query.more()) print query.next();
 +
 
 +
//Close your query object:
 +
print query.close();
 +
</pre>
 
[[Category:Developer]]
 
[[Category:Developer]]
 
[[Category:Server]]
 
[[Category:Server]]
 
[[Category:Language Bindings]]
 
[[Category:Language Bindings]]

Revision as of 13:15, 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

// Create a session object
Session session = new Session("localhost", 1984, "admin", "admin");

//Define a query and create a query object
String q = "declare variable $name external; for $i in 1 to 10 return element { $name } { $i }"
Query query = session.query(q);

//Call the bind method of your query object
query.bind("$name", "Number");

//Call the init method of your query object
print query.init();

//Iterate through the query object
while(query.more()) print query.next();

//Close your query object:
print query.close();