[ Previous ] [ Contents ] [ Index ] [ Next ]

Example 6: getemps

The following example script shows how to query a table in the database.

This example can be found in the examples/tcl/getemps.tcl file.

    
    # Example 6: Getting data from the database
    #
    # /example/getemps queries a database in the default
    # pool and returns a list of all the employees listed
    # in the employees table.  It assumes a table called
    # employees exists with the column emp_name. You can 
    # use the /NS/Db/Admin to create the table.
    #
    # Note: You must have the ns_db module loaded into your virtual
    #       server for this example to work.
    #
    # Things to notice:
    # 
    # * Use "ns_db gethandle" to get a handle for the database
    #   from the default database pool of the virtual server.
    #
    # * Use "ns_db select" to query the database and
    #   "ns_db getrow" to retrieve data.
    # 
    # * Rows are returned as ns_sets.
    #
    
    ns_register_proc GET /example/getemps getemps
    
    proc getemps {conn context} {
            set ul "<UL>"
            set db [ns_db gethandle [ns_config [ns_dbconfigpath] "DefaultPool"]]
            set row [ns_db select $db \
            "select emp_name from employees order by emp_name;"]
            while { [ns_db getrow $db $row] } {
                append ul "<LI>[ns_set get $row emp_name] \n"
            }
            append ul "</UL>"
            ns_returnnotice $conn 200 "Employee list" $ul
    }
    

Top of Page

[ Previous ] [ Contents ] [ Index ] [ Next ]
Copyright © 1996 America Online, Inc.