Previous Next Table of Contents

8. The tcsq low level database access layer

sqlscreens uses an intermediate code layer to access the different databases in a consistent fashion. This intermediate layer is called tcsq.

The tcsq calls which are documented here may be useful as a database access layer for non-sqlscreens applications (for any TCL script accessing the supported databases), or in auxiliary routines inside an sqlscreens application (for example, for building lists of values by querying a table).

8.1 Environment variables

tcsq uses the same SQLDBTYPE environment variable as the sqlscreens layer to define the database type.

8.2 API calls

tcsqconnect


tcsqconnect [host [user [passwd]]] 
 

Returns a server connection handle (hdl in the following). Depending on the database type, it may actually connect to a server, or do nothing (Ex: informix).

tcsquse


tcsquse hdl dbname
 

Associates the connection handle hdl with database dbname .

tcsqconuse


tcsqconuse database [host]
 

Utility function: connect and use.

tcsqopensel


tcsqopensel hdl stmt
 

Opens a query operation. stmt is a string holding an SQL SELECT statement.

Returns a select handle (selhdl in the following).

tcsqrew


tcsqrew selhdl 
 

Rewinds the query associated with selhdl. This may actually rerun the query (INFORMIX) or be purely local (MYSQL).

tcsqclosel


tcsqclosel selhdl
 

Closes a query, and frees the associated resources.

tcsqnext


tcsqnext selhdl
 

Returns the next row for the query, as a list of values, in the order of the columns in the SELECT statement. The last fetch returns an empty list.

tcsqexec


tcsqexec hdl stmt 
 

Executes a non-SELECT SQL statement (Ie, INSERT, DELETE, etc...)

tcsqdiscon


tcsqdiscon hdl 
 

Disconnects and frees resources associated with hdl.

tcsqtabinfo


tcsqtabinfo hdl 
 

Returns a list of the user tables in the database referenced by hdl.

tcsqcolinfo


tcsqcolinfo hdl tbl arnm 
 

Returns information about table tbl into the array the name of which is specified by arnm.

tcsqinsertid


tcsqinsertid hdl 
 

Returns the auto_increment value for the last inserted row.

tcsqquotequote


tcsqquotequote s 
 

Returns a suitably escaped string, for use in sql statements.

tcsqquoteblob


tcsqquoteblob s 
 

Same for blobs.

8.3 Programming example

The following shows is a small program to search for a name in a MYSQL 'user' table. It is not supposed to be useful for any purpose but as an example.


#!/usr/local/cdkit/isqltcl
package require tcsq
set env(SQLDBTYPE) MYSQL

set hdl [tcsqconuse mysql localhost]

set uname [tcsqquotequote "John O'Connell"] 
set qry [tcsqopensel $hdl "SELECT host,user FROM user WHERE user LIKE '$uname'"]

for {set res [tcsqnext $qry]} {$res != ""} {set res [tcsqnext $qry]} {  

    set host [lindex $res 0]  
    set user [lindex $res 1]  

    puts "Host: $host User: $user" 
} 

tcsqclosel $qry 
tcsqdiscon $hdl 
exit 0
 


Previous Next Table of Contents