Richard Bucker

Connect Tcl to Sql Server

Posted at — Jun 22, 2020

SQL Server is just no fun to connect to. Sure there are choices like the TDS protocol and ODBC API the problem is that most languages implement SQL Server last. I have no idea if the issues is that TDS or ODBC are awesome or native APIs are simply too hard to implement.

While that’s all about languages it’s even worse with operating systems. For years I have been trying to deploy FreeTDS and UnixODBC and have failed time after time.

Now as the Authors of (go)[http://golang.org] are considering generics I’m starting to consider whether go is the language for me. But that’s a separate discussion.

Here are some steps that I used ot get my tcl app (on FreeBSD 12.1) to run a query against a remote API.

NOTE

examples

my config files

/home/user/.odbc.ini

[f1]
Driver=FreeTDS
Description=<just a description>
Trace=No
Server=<ip or hostname>
Database=<databasename>
user=<dbuser>
port=1433
encryption = require

/usr/local/etc/odbcinst.ini

[FreeTDS]
  Driver = /usr/local/lib/libtdsodbc.so
  Client Charset = UTF-8
  UsageCount = 1

/usr/local/etc/freetds/freetds.conf

[f1]
 host = <ip or hostname>
 port = 1433
 encryption = require

my tcl test

package require tdbc::odbc
set connStr "Driver=FreeTDS;DSN=f1;UID=<dbuser>;PWD=<dbpassword>"
puts $connStr
tdbc::odbc::connection create db $connStr
db foreach rec { SELECT top 10 firstname FROM customers  } {
    puts "Found \"[dict get $rec firstname]\" in the customers table"
}

Running my test program

$ tclsh8.7 ss.tcl 

Conclusion

Now that I have this working for TCL I need to try this in (nim)[https://nim-lang.org/]