将TCL脚本与SQL一起使用

时间:2015-07-30 11:19:17

标签: tcl

我想编写一个TCL脚本,我需要使用MySQL。我必须(1)读取一个文件;从那(2)将数据复制到SQL表中,之后(3)根据需求从该表中查询数据。但我无法找到如何将数据从文件复制到SQL表,然后如何在TCL中查询该表。

1 个答案:

答案 0 :(得分:0)

        LOAD DATA INFILE 'path/filename.csv' #loading data from a csv file
        INTO TABLE tablename #tablename is user defined
        FIELDS TERMINATED BY ',' #csv files use comma separated values
        LINES TERMINATED BY '\n' #till the last column
        IGNORE 1 LINES #ignores the first row if you need



        proc mysql_connect {} {
        variable mysql_dbh ; variable command

        set mysql_host "hostwebsite"
        set mysql_user "username"
        set mysql_password "password"
        set mysql_db "databasename"

        ## loading the driver
        set libmysql_path "driver path"
        if {[catch {load $libmysql_path}]} {
            puts "$command Error: Unable to load mysql file" ; exit
        }

        ## making connection with mysql db
        if {[catch {set ::mysql_dbh [::mysql::connect -h $mysql_host  -u $mysql_user -password $mysql_password -db $mysql_db]}]} {
            puts "$command Error: Unable to connect mysql DB"; exit
        }
        puts "Db connected"
    }     
       proc QueryDisplay {} {

        mysql_connect

        set rows [::mysql::sel $::mysql_dbh "select *from tablename" -list]

        foreach data $rows {
            puts $data
        }

    }
    proc mysql_disconnect {} {
    variable mysql_dbh
    ::mysql::close $::mysql_dbh
}