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

SQL Anywhere 11.0.0 » SQL Anywhere Server - Programming » SQL Anywhere Data Access APIs » SQL Anywhere web services

 

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

    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.

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

  3. Start a web browser.

  4. Browse to the URL [external link] 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

  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.

    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.

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

  4. Start a web browser.

  5. Browse to the URL [external link] http://localhost:80/demo/XMLtable. Use the port number you specified when starting the database server.

    • localhost:80 Defines the web host name and port number to use.
    • demo Defines the database name to use. You are using c:\webserver\demo.db.
    • XMLtable Defines the service name to use.

    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 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();
    }
  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/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

  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.

    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.

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

  4. Start a web browser.

  5. Browse to the URL [external link] http://localhost:80/demo/JSONtable. Use the port number you specified when starting the database server.

    • localhost:80 Defines the web host name and port number to use.
    • demo Defines the database name to use. You are using c:\webserver\demo.db.
    • JSONtable Defines the service name to use.

    Your web browser should permit you to save the JSON response document returned by the database server. Save the response to a file.

  6. 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"
            }
    ]
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/.

See also