Difference between revisions of "Conf/Examples/DatabseExampleZeroOrOneRow"

From AOLserver Wiki
Jump to navigation Jump to search
(Created page with ' part of AOL Database Configuration with PostgreSQL <html> <head><title>Testing of Database connection</title></head> <body> A simple…')
 
 
(One intermediate revision by the same user not shown)
Line 7: Line 7:
 
   <%  
 
   <%  
 
   set pool "postgres_pool"
 
   set pool "postgres_pool"
   set sql "select * from users"     
+
   set sql "select * from users where user_id = 1"     
 
   set db [ns_db gethandle $pool]
 
   set db [ns_db gethandle $pool]
 
    
 
    
 
   set rec [ns_db 0or1row $db $sql]
 
   set rec [ns_db 0or1row $db $sql]
   ns_puts [ns_set array $rec]
+
   if { $rec eq "" } {
 +
      # no rows returned
 +
  } else {
 +
    # 1 row returned
 +
    ns_puts [ns_set array $rec]
 +
  }
 
   ns_db releasehandle $db
 
   ns_db releasehandle $db
 
   %>
 
   %>

Latest revision as of 15:50, 17 January 2012

part of AOL Database Configuration with PostgreSQL

<html>
   <head><title>Testing of Database connection</title></head>
 <body>  
 A simple tcl script example of Database Configuration
 <% 
  set pool "postgres_pool"
  set sql "select * from users where user_id = 1"    
  set db [ns_db gethandle $pool]
  
  set rec [ns_db 0or1row $db $sql]
  if { $rec eq "" } {
     # no rows returned
  } else {
    # 1 row returned
    ns_puts [ns_set array $rec]
  }
  ns_db releasehandle $db
 %>
 </body>
 </html>

Note

This command(ns_db 0or1row) expects the SQL to be a select statement that returns exactly zero or one row. On zero rows, a null string is returned. On one row, a newly allocated ns_set is returned. An error is thrown if more then one row is returned.