How to define a driver for tclodbc?

天涯浪子 提交于 2019-12-11 11:04:37

问题


I want to use tclodbc from Linux environment to connect to a MS SQL server. I have the driver (freeTDS) and the connection string. But I don't know how to configure the driver to be used by tclodbs. There is a command

database configure operation driver attributes

But I don't know what to put as operation and attributes, and whether this is the right command.

Related to my question: Accessing Microsoft SQL Server from Tcl running on GNU/Linux


回答1:


OK, here's my take based on these guides about how to make a DSN-less connection using the FreeTDS driver.

I've tested it a Debian Lenny system having tclodbc 2.5-5, unixodbc 2.2.11 and libdbd-freetds 0.8.2-1-4.1 and tcl 8.4.16-2 installed against an instance of Microsoft SQL Server 2005.

package require tclodbc

proc cs_append {varName args} {
    set alen [llength $args]
    if {$alen < 2 || $alen % 2 != 0} {
        return -code error "Wrong # args: should be varName key value ?key value?"
    }

    upvar 1 $varName qs

    foreach {key value} $args {
        if {$qs ne ""} {
            append qs \;
        }
        append qs $key = \{ [string map {\{ \\\{} $value] \}
    }
}

set user test
set pass secret

set cs ""
cs_append cs DRIVER FreeTDS UID $user PWD $pass \
    Server myserver.domain.local \
    ClientCharset UTF-8 \
    APP "My test app"

database connect db $cs
foreach row [db {select * from MyDatabase..MyTable}] {
    puts $row
}
db disconnect

Some notes:

  • The FreeTDS driver must be known to the ODBC subsystem by means of it being registeded in the /etc/odbcinst.ini file. I suppose that at least on my system appropriate packages take care of this by themselves but you'd better verify if you have FreeTDS registered in that file, otherwise that DRIVER=FreeTDS bit in the connection string won't work as ODBC will have no idea how to load the named driver library.
  • The ClientCharset and APP connection string parameters do not work in my case. While I can live with the second, the first one sucks big time because in this case the character data is returned in some botched encoding.

    But there's no such problem when I use named server from the /etc/freetds/freetds.conf file using the ServerName=THAT_SERVER instead of Server=SERVER_HOST in the connection string. Unfortunately, this kind of defeats half of the purpose of using DSN-less setup.

    Quite possibly it's a bug in my version of the FreeTDS driver, and I have a really outdated system here, so YMMV and you better check yourself on your system.




回答2:


If we look at the documentation, we see that there are 6 operations, of which the one you probably want is add_dsn. An example is included (below, with a small correction):

set driver "Microsoft Access Driver (*.mdb)" 
set attributes [list "DSN=mydsn" "DBQ=c:\mydb.mdb" "FIL=MS Access"] 
database configure add_dsn $driver $attributes 

I'm afraid you'll have to consult the FreeTDS documentation to get the right collection of attributes, but I think (based on this evidence) that you'll have the driver being FreeTDS and the attributes might be OK if it is an empty list (or with just TDS_Version=5.0 in it). I really don't know too much about configuring ODBC…



来源:https://stackoverflow.com/questions/10829621/how-to-define-a-driver-for-tclodbc

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!