Difference between revisions of "Command-Line Client"

From BaseX Documentation
Jump to navigation Jump to search
Line 5: Line 5:
  
 
=Startup=
 
=Startup=
 +
 
First of all, please launch a '''standalone''' version of BaseX: double click on the '''BaseX''' icon, or run the <code>basex</code> script. [[Startup#Standalone|Follow this link]] for more information (or check out the additional [[Command-Line Options#Standalone|command-line options]]).
 
First of all, please launch a '''standalone''' version of BaseX: double click on the '''BaseX''' icon, or run the <code>basex</code> script. [[Startup#Standalone|Follow this link]] for more information (or check out the additional [[Command-Line Options#Standalone|command-line options]]).
  
==Working with the BaseX Console==  
+
=Create a database=
+
 
After the BaseX Console has been started, the <code>HELP</code> command can be used to
+
* To create a database you need an XML document, e.g. [http://files.basex.org/xml/factbook.xml factbook.xml].
to list all [[Commands|database commands]]. Multiple commands can be separated by semicolons.
+
* Save this document to the directory you are working in.
 +
* In the client terminal, type in:
 +
<code>
 +
> [[Commands#CREATE DB|CREATE DB]] factbook factbook.xml
 +
</code>
 +
 
 +
: '''factbook''' is the name of the database <br/>
 +
: '''factbook.xml''' is the xml file, which is used to create the database<br/>     
 +
 
 +
If everything works you see the following lines:
 +
<pre>Database 'factbook' created in 950.83 ms.
 +
</pre>
 +
 +
; Where is the database stored?
 +
By default, databases are stored in the <code>BaseXData</code> directory, which is located in your home folder.
 +
Depending on your [[Configuration]], the location of your home folder varies. For example, on a Mac it's <code>/Users/John</code>, if your name is John. If you have used the Windows Installer, the directory will be named <code>data</code>, and reside in the application directory.
 +
 
 +
=Execute a query=
 +
The [[Commands#XQUERY|XQUERY]] command lets you run a query.
 +
* For example, this query returns all country nodes in the currently opened database.
 +
<code>
 +
> [[Commands#XQUERY|XQUERY]] //country
 +
</code>
 +
 
 +
* You can also run queries in files:
 +
<code>
 +
> [[Commands#RUN|RUN]] /Users/John/query.xq
 +
</code>
 +
 
 +
=Create a new database=
 +
Now we will create another database from the [http://files.basex.org/xml/xmark.xml xmark.xml] document.
 +
 
 +
* Create the new database, named 'xmark'.
 +
<code>
 +
> [[Commands#CREATE DB|CREATE DB]] xmark xmark.xml
 +
</code>
 +
 
 +
* Set the new database xmark as the context:
 +
<code>
 +
> [[Commands#OPEN|OPEN]] xmark
 +
</code>
 +
 
 +
* Now you can easily execute queries on your new database:
 +
<code>
 +
> [[Commands#XQUERY|XQUERY]] //people/person/name
 +
</code>
 +
 
 +
=Switch the database=
 +
* You can explicitly query the factbook database with the <code>doc(...)</code> funtion, no matter what the current context is.
 +
<code>
 +
> [[Commands#XQUERY|XQUERY]] doc("factbook")//country
 +
</code>
 +
 
 +
* Otherwise, to set factbook as the current context, execute the following:
 +
<code>
 +
> [[Commands#OPEN|OPEN]] factbook
 +
</code>
 +
 
 +
* The following command lists all databases than can be opened by the currently logged in users:
 +
<code>
 +
> [[Commands#LIST|LIST]]
 +
</code>
 +
 
 +
=Close or delete a database=
 +
* To [[Commands|close]] the current context database, please type:
 +
<code>
 +
> [[Commands#CLOSE|CLOSE]]
 +
</code>
 +
 
 +
* Use the [[Commands#DROP DB|DROP DB]] command to delete the xmark database:
 +
<code>
 +
> [[Commands#DROP DB|DROP DB]] xmark
 +
</code>
 +
 
 +
=Create a collection=
 +
'''What is a collection?'''
 +
With BaseX you can group documents into one logical collection. A collection is a database that contains two or more documents. Collections accept any type of XML documents, regardless of their structure.
 +
 
 +
Let's add the xmark.xml document to the factbook database to create a collection. The name of the original factbook database remains.
 +
 
 +
* First make sure factbook is opened:
 +
<code>
 +
> [[Commands#OPEN|OPEN]] factbook
 +
</code>
 +
 
 +
* Now add the xmark.xml document:
 +
<code>
 +
> [[Commands#ADD|ADD]] xmark.xml
 +
</code>
 +
 
 +
=Delete a document=
 +
* Deleting a document from a collection is easy:
 +
<code>
 +
> [[Commands#DELETE|DELETE]] xmark.xml
 +
</code>
 +
 
 +
Make sure that the collection, which contains the '''xmark.xml''' document, is opened.
  
To evaluate commands without entering the console mode, you can use the
+
=Delete a collection=
<code>-c</code> option on the command line:
+
Deleting a collection is the same as deleting a database.
 
<pre>
 
basex -Vc "CREATE DB input <example/>; XQUERY /"
 
  
Database 'input' created in 124.95 ms.
+
* To delete the collection factbook, type:
<example/>
+
<code>
 +
> [[Commands#DROP DB|DROP DB]] factbook
 +
</code>
  
Query: /
+
=Get server information=
 +
Several commands help to explore the state of a server. For a complete list, please visit the [[Commands]] Section.
  
Compiling:
+
* To see all databases on the server, type:
 +
<code>
 +
> [[Commands#LIST|LIST]]
 +
</code>
  
Result: root()
+
* To see the general information of the opened database, type:
 +
<code>
 +
> [[Commands#INFO|INFO]]
 +
</code>
  
Parsing: 0.42 ms
+
* To list all sessions that are managed by the server instance, type:
Compiling: 9.3 ms
+
<code>
Evaluating: 0.35 ms
+
> [[Commands#SHOW USERS|SHOW USERS]]
Printing: 5.53 ms
+
</code>
Total Time: 15.62 ms
 
  
Hit(s): 1 Item
+
=Backup and restore=
Updated: 0 Items
+
* To backup your database, type:
Printed: 10 Bytes
+
<code>
 +
> [[Commands#CREATE BACKUP|CREATE BACKUP]] factbook
 +
</code>
  
Query executed in 15.62 ms.
+
* To restore your database, type:
</pre>
+
<code>
 +
> [[Commands#RESTORE|RESTORE]] factbook
 +
</code>
 +
 
 +
'''Where is the backup-file stored?'''
  
All available command-line options can be found [[Command-Line Options#BaseX Standalone|here]].
+
The backup-file is stored in the database directory.
 +
The file is named <code>factbook-timestamp.zip</code> (<code>db_name-timestamp.zip</code>).
 +
To restore the database the file with the newest timestamp is taken.
  
 
[[Category:Beginner]]
 
[[Category:Beginner]]

Revision as of 10:06, 12 August 2015

This page is part of the Getting Started Section. BaseX offers a standalone console mode from which all database commands can be executed. The article on the Database Server provides numerous examples for running commands in the console mode (note that the GUI does not interact with the client/server architecture).

Startup

First of all, please launch a standalone version of BaseX: double click on the BaseX icon, or run the basex script. Follow this link for more information (or check out the additional command-line options).

Create a database

  • To create a database you need an XML document, e.g. factbook.xml.
  • Save this document to the directory you are working in.
  • In the client terminal, type in:

> CREATE DB factbook factbook.xml

factbook is the name of the database
factbook.xml is the xml file, which is used to create the database

If everything works you see the following lines:

Database 'factbook' created in 950.83 ms.
Where is the database stored?

By default, databases are stored in the BaseXData directory, which is located in your home folder. Depending on your Configuration, the location of your home folder varies. For example, on a Mac it's /Users/John, if your name is John. If you have used the Windows Installer, the directory will be named data, and reside in the application directory.

Execute a query

The XQUERY command lets you run a query.

  • For example, this query returns all country nodes in the currently opened database.

> XQUERY //country

  • You can also run queries in files:

> RUN /Users/John/query.xq

Create a new database

Now we will create another database from the xmark.xml document.

  • Create the new database, named 'xmark'.

> CREATE DB xmark xmark.xml

  • Set the new database xmark as the context:

> OPEN xmark

  • Now you can easily execute queries on your new database:

> XQUERY //people/person/name

Switch the database

  • You can explicitly query the factbook database with the doc(...) funtion, no matter what the current context is.

> XQUERY doc("factbook")//country

  • Otherwise, to set factbook as the current context, execute the following:

> OPEN factbook

  • The following command lists all databases than can be opened by the currently logged in users:

> LIST

Close or delete a database

  • To close the current context database, please type:

> CLOSE

  • Use the DROP DB command to delete the xmark database:

> DROP DB xmark

Create a collection

What is a collection? With BaseX you can group documents into one logical collection. A collection is a database that contains two or more documents. Collections accept any type of XML documents, regardless of their structure.

Let's add the xmark.xml document to the factbook database to create a collection. The name of the original factbook database remains.

  • First make sure factbook is opened:

> OPEN factbook

  • Now add the xmark.xml document:

> ADD xmark.xml

Delete a document

  • Deleting a document from a collection is easy:

> DELETE xmark.xml

Make sure that the collection, which contains the xmark.xml document, is opened.

Delete a collection

Deleting a collection is the same as deleting a database.

  • To delete the collection factbook, type:

> DROP DB factbook

Get server information

Several commands help to explore the state of a server. For a complete list, please visit the Commands Section.

  • To see all databases on the server, type:

> LIST

  • To see the general information of the opened database, type:

> INFO

  • To list all sessions that are managed by the server instance, type:

> SHOW USERS

Backup and restore

  • To backup your database, type:

> CREATE BACKUP factbook

  • To restore your database, type:

> RESTORE factbook

Where is the backup-file stored?

The backup-file is stored in the database directory. The file is named factbook-timestamp.zip (db_name-timestamp.zip). To restore the database the file with the newest timestamp is taken.