mysqltcl

Difference between version 36 and 39 - Previous - Next
'''[http://www.xdobry.de/mysqltcl/%|%mysqltcl]''' is a simple interface to [MySQL] for [Tcl]


** Attributes **

   current version:   3.052

   release time:   2012-10

** Binary packages **

   For Windows:   http://www.xdobry.de/mysqltcl/mysqltcl-3.03.zip   For Mac OSX:   http://web.archive.org/web/https://www.zolli.fr/fichiers/mysqltcl-3.03.zip


** See Also **

   [adb], by [Roalt Aalmoes]:   a simple tcl database engine that uses mysqltcl underneath.


** Documentation **

   [http://linuxfocus.org/English/March2004/article331.shtml%|%An introduction to the TclMySQL library], Diego Alberto Arias Prad, 2004-03:   


** Description **

Tcl interface to [mysql] relational [database].  Supports Tcl 8 objects,
unicode, nested queries, etc.  Currently at version 3.05.


** Example 1 **
[ATK] 2018-JUN-05: I've used mysqltcl before, very simple and nice interface to mysql db.
Here's my sample proc for creating a DB connection.
======
package require mysqltcl
proc MySQLConnect {db_name db_user db_passwd} {
        # open the database connection with the parameters supplied
        set dbhandle {}
        if {[catch {set dbhandle [mysqlconnect -port 3306 -user $db_user -password $db_passwd -db $db_name]} cerr]} {
                set dbhandle {}
        }
        return $dbhandle
} ;#endOf::MySQLConnect

======

[SS] 2005-02-12:   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
======

[Artur Trzewik]: Some comments about example:

`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-09-10: So, is it really mysql::rec''ie''ve rather than
mysql::rec''ei''ve ?


** Example 2, test transaction **


[Str] 20141119-2248-strobel: I wrote a bigger sample code for c.l.tcl testing the transactional 
abilites of the storage engines. Enjoy.
======
#!/usr/bin/env tclsh

package require mysqltcl

proc getdb {} {
   global thisisthedbhandle
   if {![info exists thisisthedbhandle]} {
      set thisisthedbhandle [::mysql::connect -db yourdb -user you -password yourpass]
      ::mysql::autocommit $thisisthedbhandle false
   }
   return $thisisthedbhandle
}
proc getflat {sql} {
   return [::mysql::sel [getdb] $sql -flatlist]
}
proc dbcmd {sql} {
   return [::mysql::exec [getdb] $sql]
}
proc create1 {eng} {
  dbcmd "create table if not exists t1 (id integer auto_increment primary key, 
                   val integer not null default 0) engine = $eng"
  dbcmd "create table if not exists t2 (counter integer) engine = $eng"
  dbcmd "insert into t2(counter) values (0);"
}
proc drop1 {} {
  dbcmd "drop table if exists t1;"
  dbcmd "drop table if exists t2;"
}
proc showt {t} {
  switch $t {
   t1 { foreach {i e} [getflat "select * from t1"] {
          puts "id: $i\tvalue: $e" }
       }
    t2 { foreach e [getflat "select * from t2"] { 
            puts "sum in table 2: $e"  }
       }
  }
}

proc testit {} {
  # indeed, there is no start transaction in the interface, just do it by hand
  dbcmd "start transaction;"
  dbcmd "insert into t1 (val) values (4);"
  dbcmd "update t2 set counter = counter + 4;"
  dbcmd "commit;"
  puts "checking values are there:"
  showt t1
  showt t2
  puts "insert into t1 then rollback - result:"
  dbcmd "start transaction;"
  dbcmd "insert into t1 (val) values (42);"
  # why should i do ::mysql::rollback, it is too much to write
  dbcmd "rollback"
  showt t1
  showt t2
}
#--------------------------------
puts "test with MyISAM"
create1 MyISAM
testit 
drop1

puts "test with InnoDB"
create1 InnoDB
testit 
drop1
# ------------- end of tests, and glorious evening..
proc sing {} {
  puts "
    You can't always get what you want,
    You can't always get what you want,
    But if you try sometime,
    You might find 
    You get what you need!
 "
}
sing
======




** Mysqltcl and [Starkit%|%Starkits] **

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]
======


** Misc **

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

I am also looking for same type of solution.

<<categories>> Package | Database | Library | SQL | MySQL