Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.

SQL Anywhere 10.0.1 » SQL Anywhere Server - Programming » SQL Anywhere Web Services

Introduction to web services Next Page

Quick start to web services


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 XML web service
  1. Copy the SQL Anywhere sample database from samples-dir to another location, such as c:\webserver\demo.db.

  2. 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.

    dbeng10 -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.

  3. 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 XMLtables
    TYPE 'XML'
    AUTHORIZATION OFF
    USER DBA
    AS SELECT * FROM Customers

    This statement creates a web service named XMLtables. 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.

  4. Start a web browser.

  5. Browse to the URL [external link] http://localhost:80/demo/XMLtables. 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.

  6. You can also access the XMLtables routine from common programming languages. For example, the following short C# program uses the XMLtables web service:

    using System.Xml;
    
    static void Main(string[] args)
    {
      XmlTextReader reader =
        new XmlTextReader( "http://localhost:80/demo/XMLtables" );
    
      while( reader.Read() )
      {
        switch( reader.NodeType )
        {
        case XmlNodeType.Element:
          if( reader.Name == "row" )
          {
            Console.Write(reader.GetAttribute("ID")+" ");
            Console.WriteLine(reader.GetAttribute("Surname"));
          }
          break;
        }
      }
    }
  7. 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/XMLtables' )

    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 HTML web service
  1. 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.

    dbeng10 -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.

  2. 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 HTMLtables
    TYPE 'HTML'
    AUTHORIZATION OFF
    USER DBA
    AS SELECT * FROM Customers

    This statement creates a web service named HTMLtables. 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.

  3. Start a web browser.

  4. Browse to the URL [external link] http://localhost:80/demo/HTMLtables. 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.

Other resources for getting started

Samples are included in the samples-dir\SQLAnywhere\HTTP directory.

Other examples might be available on CodeXchange at [external link] http://ianywhere.codexchange.sybase.com/.