Version 24 of mysqltcl

Updated 2011-09-08 23:22:21 by ZB

What: mysqltcl

 Where: http://www.xdobry.de/mysqltcl/
 Homepage: http://www.xdobry.de/mysqltcl/index.html
 Description: Tcl interface to mysql relational database.
        Supports Tcl 8 objects, unicode, nested queries, etc.
        Currently at version 3.05.
 Updated: 4/2008
 Contact: See web site

See also MySQL.


SS: 12Feb2005, I wrote this very short example of mysqltcl usage for a guy asking on the tclers chat about how to use it. I'll put it here to redirect to this page the next time people ask about it.

This code connects to the mysql server at 'localhost', logs in with the username 'root', password 'foobar', selects the database 'mysql', and executes the query "SELECT HOST FROM USER". Every host returned is printed using puts, and finally the mysql handle is closed.

 package require mysqltcl
 set m [mysqlconnect -user root -db mysql -password foobar]
 mysqluse $m mysql
 foreach res [mysqlsel $m {select host from user} -flatlist] {
     puts $res
 }
 mysqlclose $m

Some comments about example by Artur Trzewik

'mysqluse $m mysql' is not needed in this case because the database is already specified as connection parameter.

The fastest solution in mysqltcl for fetching data (bigger volumes) is mysqlmap. mysqlsel with -flatlist will build a Tcl-List with the whole result. For very big data-volumes the best choice is mysql::receive, as that does not use client caching (on the level of mysqlclient C-library). Starting with version 3.00, you can also use namespace command names.

  #using fetch 
  mysql::sel $m {select host from user}
  while {[llength [set row [mysql::fetch $m]]]>0} {
     # row is always a list. using lindex avoids conversion list to string 
     puts [lindex $row 0]
  }

  # the fastest way to operate data without building big internal Tcl lists.
  mysql::sel $m {select host from user}
  mysql::map $m host {
     puts $host
  }

  # for very big data volumes. No caching at all
  mysql::recieve $m {select host from user} host {
     puts $host
  }

LV 2007 Sep 10 So, is it really mysql::recieve rather than mysql::receive ?


Mysqltcl and starkits by DrumBSD

I found this code on a mailing list which wraps mysqltcl for Microsoft Windows into a starkit or starpack. You have to modify pkg_Index.tcl like this:

  proc loadmysqltcl { dir } {
     set oldcwd [pwd]
     cd $dir

    foreach file [glob *.dll] {
        file copy -force $file c:/winnt/temp
    }
    cd c:/winnt/temp
    load libmysqltcl[info sharedlibextension]
    cd $oldcwd
  }

   package ifneeded mysqltcl 3.01 [list loadmysqltcl $dir]

Anyway, there's a problem when "c:/winnt/temp" doesn't exists. So I tried to put a $tcl_platform(osVersion) into pkgIndex.tcl but it doesn't work. Maybe when it loads mysqltcl, the tcl_platform array doesn't exist. Any tips to solve this problem?

JH: Did you make sure to declare tcl_platform global in the proc?

ramsan: I think that all Microsoft Windows have defined the variable TEMP. So:

  proc loadmysqltcl { dir } {
     set oldcwd [pwd]
     cd $dir

    foreach file [glob *.dll] {
        file copy -force $file $::env(TEMP)
    }
    cd $::env(TEMP)
    load libmysqltcl[info sharedlibextension]
    cd $oldcwd
  }
  package ifneeded mysqltcl 3.01 [list loadmysqltcl $dir]

Roalt Aalmoes has made a simple tcl database engine called adb that uses this mysqltcl underneath.

crouzilles How do you use placeholders in mysqltcl? Can you do {select * from mytable where name = ?}?


[ Category Package | Category Database ]