A simple MySQL client in Tcl/Tk using mysqltcl

Yesterday, Ron Cripe emailed me asking if I knew of any simple examples of how to use Tcl/Tk with MySQL. I knew about mysqltcl and pointed him in that direction, but he said he needed an actual example on how to use it.

I’ve done a lot of Tcl programming but have mostly avoided doing GUI’s with Tk, so I figured this might be a good opportunity to do some learning, myself. I quickly wrote a simple application that connects to a MySQL database, then lets you execute queries against it and displays the results. You can download it here:

It’s named .txt to make it easier to download–just rename it to only .tcl (removing the .txt) once you’ve saved it.

When you start the app, it presents you with a simple login dialog:

mysqltcl-client login dialog

Enter your username and password, change the host, port and database values if necessary, and press Enter or click the Connect button. If all goes well, the login dialog will go away and the query window will appear:

mysqltcl-client query dialog

This is an example of what the query dialog looks like after executing a SHOW TABLES query. Very simple, not terribly exciting, but what do you want for a 5KB, 181-line Tcl script?

If you have Tcl, Tk and mysqltcl installed, go ahead and grab a copy of this script and check it out. If you have questions about the code, just ask: I’ll try my best to answer them.

Tags: , , , ,

Comments

  1. Hi Dossy,

    I feel so lucky to found this website..It sound interesting to me since I am also work on tcl and mysql (mysqtcl) and looking for this quite very long time. I have some problem with mysqltcl that I hope you can answer.

    OK..let me explain, I want to access the mysql database from tcl code, so I have installed mysql5.0.4 and mysqltcl3.0.2 and I already have tcl8.3.2 installed with the linux package (i am using debian). My mysql and tcl is working fine. The problem now is I can’t connect both of them using mysqltcl..I am using command “mysql::connect” but it returned “bash:command not found”..the installation was succesfull, but I dont have any idea why this happened. Actually I am totally new in mysqltcl, I just follow the command from the manual. So I want to ask you if u have any idea on how to connect them using mysqltcl..

    Thanks in advance for you time and help!!

  2. newbie36: Are you executing your Tcl script correctly? If you’re getting a “bash: command not found” error, it doesn’t sound like it. It sounds like you’re trying to execute Tcl code from your shell prompt, not as a Tcl script.

    I’ll need more specific details (i.e., see exactly what you’re doing) to really give you any help with this.

  3. Hi Dossy,

    Really appreciate your quick response!..

    Let me make you more clear. I need to store and retrieve some data into mysql database using tcl script, so I installed mysqltcl in order fo me to get access directly into the database from the tcl right?

    Ok, actually, after installing mysqltcl, am I suppose to connect both of them via the mysqltcl shell prompt like I said in the previous post (::mysql::connect), or use the command in tcl scripts? I have tried to google on how to connect them both, but very much little info I get..when I type #mysqld_safe -u root mysql, it returned : ” Cant connect to localhost through var/lib/mysqld/mysqld.sock” (and this file actually is not exist) even though I didn’t installed it here.

    And if I type #mysqld_safe, It will returned

    “Starting mysqld daemon using var/lib/mysql, STARTED”,

    but then just a few second, it will stopped by displaying

    “STOPPING the server using var/lib/mysql/ns2-box.pid” .

    I tried to point to the correct library path by using –with-mysql-var=/path/to/mysql/data during configure, but I dont know why this error still came out (even I have changed the path).

    Maybe this is not the correct way of connecting them (that’s why I got those errors). Do i need to perform this command at mysqltcl shell prompt? or it should be applied in the tcl script itself? I really need to clarify this out since I have been stucked with this about a week!..I really hope you can help me in this and I really appreciate it!..

    Sorry for my bad English, I hope you can understand.

  4. Hi,
    execute “package require mysqltcl” to check if installed. if installed it should give out the mysqltcl version like 3.03. If the command gives a error put ur mysqltcl package in tcl/lib and then try!!!!!!!!!!

  5. hello readers and author…i having some kind of difficulty..hoping that you can help me..can anyone tell me how to install mysqltcl on windows vista or point to me any resources that useful to me..i kinda stuck with this problem now in my project..tq

  6. please help me..

  7. i install my Tcl using the Activestate ActiveTcl 8.5.7..im using MySQL server 5.1…and i download the Windows Binary Version of mysqltcl with zip packed mysqltcl Release 3.05..after that i extracted the package into Tcl\lib as instructed…and i dont know what to do after that…how to compile this thing? i am really clueless right now…

    • If you are using the binary version, you don’t compile anything. At this point, you should be able to start a Tcl shell and type “package require mysqltcl” in it.

      If this is proving to be too difficult, you may want to stop trying to do this with Tcl.

      • im wondering..if i dont put the Tcl directory to the path in my system environment variable..can this cause the error? anyway..tq for you help..but since i develop my application using Tcl/Tk..its kind of hard to use another language to interface with the database..

  8. hi..
    I’ve been trying to install tclmysql on solaris 10. can you give me any tutorial about it. actually, i’m going to make login interface that connected to mysql. any help would be appreciated.. thanks

  9. I would appreciate it if you could tell me if this scrip that I have written (slightly modifies your connect function) is the right way. SA_telnet_puts is just a print command on the application that uses this script.

    proc connect {} {
        global conn
        if {[catch {
            set conn(handle) [::mysql::connect \
                -user "root" -password "root" \
                -host "127.0.0.1" -port "3306" -db "nexus_sim"]
        } err]} {
            SA_telnet_puts "Can't connect!"
    
        } else {
            SA_telnet_puts "Connected!"
        }
    }

Speak Your Mind

*