The following procedure describes how to create a new database, start a SQL Anywhere database with the HTTP server enabled, and access this database using any popular web browser.
To create and access a simple HTML web service
Execute the following statement to start a personal web server. The -xs http(port=80)
option tells the database server to listen for HTTP requests. If you already have a web server running on port 80, use another
port number such as 8080 for this demonstration.
dbeng11 -xs http(port=80) c:\webserver\demo.db |
Many properties of the HTTP communication link are controlled by parameters to the -xs option.
For more information, see -xs server option.
As well as starting the database server with the appropriate -xs option parameters, you must create web services to respond to incoming requests. These are defined using the CREATE SERVICE statement.
Start Interactive SQL. Connect to the SQL Anywhere sample database as the DBA. Execute the following statement.
CREATE SERVICE HTMLtable TYPE 'HTML' AUTHORIZATION OFF USER DBA AS SELECT * FROM Customers; |
This statement creates a web service named HTMLtable. This simple web service returns the results of the statement SELECT * FROM Customers, automatically converting the output into HTML format. Because authorization is off, no permissions are required to access the table from a web browser.
For more information, see Creating web services and CREATE SERVICE statement.
Start a web browser.
Browse to the URL http://localhost:80/demo/HTMLtable. Use the port number you specified when starting the database server.
Your web browser shows you the body of the HTML document returned by the database server. By default, the result set is formatted into an HTML table.
To create and access a simple XML web service
Copy the SQL Anywhere sample database from samples-dir to another location, such as c:\webserver\demo.db.
Execute the following statement to start a personal web server. The -xs http(port=80)
option tells the database server to listen for HTTP requests. If you already have a web server running on port 80, use another
port number such as 8080 for this demonstration.
dbeng11 -xs http(port=80) c:\webserver\demo.db |
Many properties of the HTTP communication link are controlled by parameters to the -xs option.
For more information, see -xs server option.
As well as starting the database server with the appropriate -xs option parameters, you must create web services to respond to incoming requests. These are defined using the CREATE SERVICE statement.
Start Interactive SQL. Connect to the SQL Anywhere sample database as the DBA. Execute the following statement:
CREATE SERVICE XMLtable TYPE 'XML' AUTHORIZATION OFF USER DBA AS SELECT * FROM Customers; |
This statement creates a web service named XMLtable. This simple web service returns the results of the statement SELECT * FROM Customers, automatically converting the output into XML format. Because authorization is off, no permissions are required to access the table from a web browser.
For more information, see Creating web services and CREATE SERVICE statement.
Start a web browser.
Browse to the URL http://localhost:80/demo/XMLtable. Use the port number you specified when starting the database server.
Your web browser shows you the body of the XML document returned by the database server. As no formatting information has been included, you see the raw XML, including tags and attributes.
You can also access the XMLtable service from common programming languages. For example, the following short C# program uses the XMLtable web service:
using System.Xml; static void Main(string[] args) { XmlTextReader reader = new XmlTextReader( "http://localhost:80/demo/XMLtable" ); while( reader.Read() ) { switch( reader.NodeType ) { case XmlNodeType.Element: if( reader.Name == "row" ) { Console.Write(reader.GetAttribute("ID")+" "); Console.WriteLine(reader.GetAttribute("Surname")); } break; } } reader.Close(); } |
In addition, you can access the same web service from Python, as in the following example:
import xml.sax class DocHandler( xml.sax.ContentHandler ): def startElement( self, name, attrs ): if name == 'row': table_id = attrs.getValue( 'ID' ) table_name = attrs.getValue( 'Surname' ) print '%s %s' % ( table_id, table_name ) parser = xml.sax.make_parser() parser.setContentHandler( DocHandler() ) parser.parse( 'http://localhost:80/demo/XMLtable' ) |
Save this code in a file called DocHandler.py. To run the application, enter a command like the following:
python DocHandler.py |
To create and access a simple JSON web service
Copy the SQL Anywhere sample database from samples-dir to another location, such as c:\webserver\demo.db.
Execute the following statement to start a personal web server. The -xs http(port=80)
option tells the database server to listen for HTTP requests. If you already have a web server running on port 80, use another
port number such as 8080 for this demonstration.
dbeng11 -xs http(port=80) c:\webserver\demo.db |
Many properties of the HTTP communication link are controlled by parameters to the -xs option.
For more information, see -xs server option.
As well as starting the database server with the appropriate -xs option parameters, you must create web services to respond to incoming requests. These are defined using the CREATE SERVICE statement.
Start Interactive SQL. Connect to the SQL Anywhere sample database as the DBA. Execute the following statement:
CREATE SERVICE JSONtable TYPE 'JSON' AUTHORIZATION OFF USER DBA AS SELECT * FROM Customers; |
This statement creates a web service named JSONtable. This simple web service returns the results of the statement SELECT * FROM Customers, automatically converting the output into JavaScript Object Notation format. Because authorization is off, no permissions are required to access the table from a web browser.
For more information, see Creating web services and CREATE SERVICE statement.
Start a web browser.
Browse to the URL http://localhost:80/demo/JSONtable. Use the port number you specified when starting the database server.
Your web browser should permit you to save the JSON response document returned by the database server. Save the response to a file.
If you use a text editor to view the file containing the response, you will see the following array notation for the result set.
[ { "ID": 101, "Surname": "Devlin", "GivenName": "Michaels", "Street": "114 Pioneer Avenue", "City": "Kingston", "State": "NJ", "Country": "USA", "PostalCode": "07070", "Phone": "2015558966", "CompanyName": "The Power Group" }, { "ID": 102, "Surname": "Reiser", "GivenName": "Beth", "Street": "33 Whippany Road", "City": "Rockwood", "State": "NY", "Country": "USA", "PostalCode": "10154", "Phone": "2125558725", "CompanyName": "AMF Corp." }, . . . { "ID": 665, "Surname": "Thompson", "GivenName": "William", "Street": "19 Washington Street", "City": "Bancroft", "State": "NY", "Country": "USA", "PostalCode": "11700", "Phone": "5165552549", "CompanyName": "The Apple Farm" } ] |
Samples are included in the samples-dir\SQLAnywhere\HTTP directory.
Other examples might be available on CodeXchange at http://ianywhere.codexchange.sybase.com/.
Send feedback about this page via email or DocCommentXchange | Copyright © 2008, iAnywhere Solutions, Inc. - SQL Anywhere 11.0.0 |