Version 80 of Interacting with databases

Updated 2010-11-10 20:06:54 by drh

Purpose: Serve as a collection point for work relating to interfacing between Tcl and databases.


There are a variety of issues relating to Tcl and databases. The first and most thorny, in my (LV) opinion is the selection of the right database. That is because that currently there is no generic interface to databases available in Tcl, so one has to either write multiple versions of an application, to support whatever databases the user community is going to be using, or you select one to support and force everyone to use that one, frequently by including the database code in the application itself.

Both of these are uncomfortable decisions on the part of programmers who wish widest acceptance of their application.

To find the various extensions mentioned below, visit http://www.purl.org/NET/Tcl-FAQ/part5.html .

Tom Poindexter has written interfaces to Sybase and Oracle. Several people have written interfaces to Berkeley DB, mini SQL (mSQL) and PostgreSQL. Jean-Claude Wippler has written an interface to Metakit, which is a small database designed around the idea of variable persistence. Metakit has a wonderful Tcl binding that makes all the power of Metakit available in a natural and intuitive way. OOMK constructs Metakit objects and gives even more access, together with the benefits of Snit. Jacob Levy has written the e4graph extension for integrating graph structured data storage into Metakit. Interfaces to lesser known databases such as OBST, Coral, MOREplus, Qddb, etc. "Regular Expressions" ran a column [L1 ] on "Tcl and Database Managers -- A Survey" in August 2001.

A mailing list to discuss database APIs is available at mailto:[email protected] .

A page here is devoted to generic Database Interfaces.


Several of the extensions mentioned above live on SourceForge now:

Please add any extension I forgot, AK. Thanks.


Don't forget that AOLserver has its own drivers for many databases (Oracle, PostgreSQL, Sybase, Solid, etc.), all accessible from the same ns_db Tcl API. And the AOL maintainers have started refactoring the C code to make some of this stuff shared libraries, and thus potentially very useful in non-AOLserver environments as well...


   Here are a variety of other Tcl database bindings

See also Database Interface , A simple database , BOOK Database Backed Web Sites, and the Developer Exchanged information site, http://www.purl.org/tcl/home/resource/software/extensions/database/ . Arts and crafts of Tcl-Tk programming Discussions on making data file access simpler


So why hasn't the Tcl community come to an agreement on one generic database interface in a fashion similar to Perl's DBI module? RLH 2005-07-19: Exactly!

WK Well, there's tcldb that does that. It may not be the most lightweight interface, but it seems to do the trick. It also (which IMHO is more important) handles some differences between databases (like FORMAT_DATE vs TO_CHAR, NOW vs CURRENT_* and so on). See http://dqsoftware.sourceforge.net/ , there's an upcoming release when I finally find the time for it really soon (stated 2006-05-23).


Comparing Tcl database abstraction layers

... is one way of plugging different DB backends into an app without (ahem) altering an app. A slightly different angle on this would be to aim for a common API/protocol, without completely "wrapping" the DB. -jcw

IOW, instead of creating code through which all requests are made, an OO-ish option would be to return a database handle (as some wrappers already do), and insist on a sufficiently similar set of method names and conventions that the same calls will work across different database extensions. Nothing stellar, but perhaps good enough? I know of at least one (now obsolete) wrapper which did this by emulating Tclodbc.


Basic question re: MSSQL database access

JDM 2006-08-02: One interface that I do not see above is for MS SQL Server databases. Is this because none is needed? Or because the Sybase interface will work? Or because no one has written one specifically for MS SQL so ODBC connections should be used? I apologize if I have overlooked something.

TP answers 2006-08-02: For MSSQL interface, Sybtcl should work, and I would recommend using the FreeTDS [L3 ] interface library, as it can compile options to include various MSSQL protocol versions. FreeTDS can also build an ODBC driver, so Tclodbc works too. For most of my DB interface needs lately, I've been using Tclodbc on both Unix/Linux and Windows.

JDM 2006-08-02: Many thanks for the quick answer! I will try Tclodbc. As someone new to TCL, I am looking for examples that use Tclodbc as well. Can you point me to any? Thanks in advance.

RLH A generic interface is now being seriously worked on: TDBC


The Redis project [L4 ] supports Tcl as well as other languages. The home page says of Redis: Redis is an advanced key-value store. It is similar to memcached but the dataset is not volatile, and values can be strings, exactly like in memcached, but also lists, sets, and ordered sets. All this data types can be manipulated with atomic operations to push/pop elements, add/remove elements, perform server side union, intersection, difference between sets, and so forth. Redis supports different kind of sorting abilities.

In order to be very fast but at the same time persistent the whole dataset is taken in memory, and from time to time saved on disc asynchronously (semi persistent mode) or alternatively every change is written into an append only file (fully persistent mode). Redis is able to rebuild the append only file in background when it gets too big.

The Redis source tree contains test scripts written in Tcl.