#this code prints out all products in the database# that are below a specified price (assumed to have been determined# beforehand, and stored in the variable targetprice)# the output is in html table format, appropriate for cgi output#load the sql shared object library. the tcl interpreter could also#have been compiled with the library, making this line unnecessaryload /home/aroetter/tcl-sql/sql.so#these are well defined beforehand, or they could#be passed into the scriptset dbname "clientwebsite";set tblname "products";set dbhost "backend.company.com"set dbuser "mysqluser"set dbpasswd "abigsecret"set targetprice 200;#connect to the databaseset handle [sql connect $dbhost $dbuser $dbpasswd]sql selectdb $handle $dbname ;# get test database#run a query using the specified sql codesql query $handle "select * from $tblname where price <= $targetprice"#print out html table headerputs "<table border=4>"puts "<th>product id <th width=200>description <th>price (/$)"#output table rows - each fetchrow retrieves one result#from the sql querywhile {[set row [sql fetchrow $handle]] != ""} { set prodid [lindex $row 0] set descrip [lindex $row 1] set price [lindex $row 2] puts "<tr><td>$prodid <td align=center>$descrip <td>$price"}puts "</table>"#empty the query result buffer - should already be empty in this casesql endquery $handle#close the db connection - in practice this same connection#is used for multiple queriessql disconnect $handle
新闻热点
疑难解答