Generic Tcl Database Interface API

$Id: api.html,v 1.8 1999/08/13 19:44:55 tdarugar Exp $

Your code must load the sql package using the normal method: package require sql .
Alternatively you can simply load the shared object using something like: load ./sql.so

The sql package adds the command sql to tcl. This command takes several subcommands, as listed below. The subcommands are always the first argument to the sql command. The second argument, if necessary, is the handle for a given connection.

MySQL specific functions are access via the mysql command.

Normal usage would be to create a connection via connect, select a database via selectdb, query or execute sql commands via query and exec, close the query via endquery, and close the connection using disconnect .

sql

connect <host> <userid> <password>
Connect to the database. Returns the handle from that connection.
Takes two optional arguments, the userid and password.
set conn [sql connect]
set conn2 [sql connect host someuser somepassword]
disconnect handle
Disconnect the given connection.
sql disconnect $conn
selectdb handle dbname
Select the dbname database to be used on the given handle. After doing a connect you must selectdb.
sql selectdb $conn mydatabase
exec handle statement
Execute a sql statement.
sql exec $conn "create table sample (x integer)"
query handle statement
Query the database for some results (generally a select statement). Use with fetchrow and endquery.
Returns handle to result set, to be used when fetching rows with fetchrow.
sql query $conn "select * from sample where x > 3"
set res_handle [sql query $conn "select * from sample where x > 3"]
endquery connection_handle <result_handle>
End a query. Must be called once the query is completed and the results have been fetched. sql endquery $conn $res_handle
fetchrow connection_handle <result_handle>
Fetch the next row of results. Must be executed after a query statement. The row is returned as a tcl list. If there are no more rows available an empty string is returned.
The result_handle parameter is optional; if only one query is issued at a time it is not necessary.
while {[set row [sql fetchrow $conn]] != ""} { puts $row }
while {[set row [sql fetchrow $conn $res_handle]] != ""} { puts $row }
numrows connection_handle result_handle (New command)
Returns the number of rows returned on result_handle.
In previous versions query returned the number of rows. It now returns the result handle (in order to allow multiple queries on the same connection), so the numrows command was added.
set num_rows_returned [sql numrows $conn $res_handle]

mysql

escape some_string
Escape the given string. Equivalent to MySQL's mysql_escape_string() function.
set cmd [mysql escape {Some String}]

Sample Usage:

Also see sample.simple.txt and sample.full.txt .

# Load the sql package:
package require sql

# Connect to the database
set conn [sql connect]

# select the database 'test' to be used.
sql selectdb $conn test

# Create a table and put some data in it:
sql exec $conn "create table junk (i integer, r real, s char(10))"

# Put some dummy data in:
for {set i 0} {$i < 10} {incr i} {
	sql exec $conn "insert into junk values ($i, $i.01, 'xx $i xx')"
}

# Do a select and display the results
sql query $conn "select * from junk where i > 3" 

while {[set row [sql fetchrow $conn]] != ""} {
	puts "row = $row"
}

sql endquery $conn

sql exec $conn "drop table junk"
sql disconnect $conn

Results:
row = 4 4.0100 {xx 4 xx}
row = 5 5.0100 {xx 5 xx}
row = 6 6.0100 {xx 6 xx}
row = 7 7.0100 {xx 7 xx}
row = 8 8.0100 {xx 8 xx}
row = 9 9.0100 {xx 9 xx}