2 # Write and read file into database
5 # The output file will be written with file.bin
7 if {[file exists libload.tcl]} {
10 source [file join [file dirname [info script]] libload.tcl]
14 if {[llength $argv]==0} {
15 puts "usage. binarytest.tcl filename. File name should be binary test file that will be put in Datebase read from it and save als filename.bin"
19 set file [lindex $argv 0]
20 set fhandle [open $file r]
21 fconfigure $fhandle -translation binary -encoding binary
22 set binary [read $fhandle]
25 puts "test conection without encoding option"
27 # What is saved in mysql is dependend from your system encoding
28 # if system encoding is not utf-8 then data could not be safed correctly
29 # It is also not so good idea to safe binary data as utf-8.
30 # I recommend always to use -encoding binary if you handle binary data
31 # You can alway build multiple handles if you want handle binary and utf-8 data.
33 set handle [mysqlconnect -user root -db uni]
34 mysqlexec $handle "INSERT INTO Binarytest (data) VALUES ('[mysqlescape $binary]')"
35 set id [mysqlinsertid $handle]
37 set nfile [file tail $file].bin
38 set fhandle [open $nfile w]
39 fconfigure $fhandle -translation binary -encoding binary
40 #set nbinary [lindex [lindex [mysqlsel $handle "SELECT data from Binarytest where id=$id" -list] 0] 0]
41 mysqlsel $handle "SELECT data from Binarytest where id=$id"
42 #set nbinary [encoding convertfrom [encoding system] [lindex [mysqlnext $handle] 0]]
43 set nbinary [lindex [mysqlnext $handle] 0]
44 puts "primary length [string bytelength $binary] new length [string bytelength $nbinary] - [string length $binary] [string length $nbinary]"
45 puts -nonewline $fhandle $nbinary
47 if {[catch {exec cmp $file $nfile}]} {
48 puts "binary comparing failed primary length [file size $file] new length [file size $nfile]"
50 puts "binary comparing ok"
52 puts "Length in Mysql [mysqlsel $handle "SELECT LENGTH(data) from Binarytest where id=$id" -flatlist]"
55 puts "test with -encoding binary"
56 set handle2 [mysqlconnect -user root -db uni -encoding binary]
57 mysqlexec $handle2 "Update Binarytest set data = '[mysqlescape $binary]' where id=$id"
58 mysqlsel $handle2 "SELECT data from Binarytest where id=$id"
59 set nbinary [lindex [mysqlnext $handle2] 0]
61 set nfile [file tail $file].bin
62 set fhandle [open $nfile w]
63 fconfigure $fhandle -translation binary -encoding binary
64 puts "primary length [string bytelength $binary] new length [string bytelength $nbinary] - [string length $binary] [string length $nbinary]"
65 puts -nonewline $fhandle $nbinary
67 if {[catch {exec cmp $file $nfile}]} {
68 puts "binary comparing failed primary length [file size $file] new length [file size $nfile]"
70 puts "binary comparing ok"
72 puts "Length in Mysql [mysqlsel $handle2 "SELECT LENGTH(data) from Binarytest where id=$id" -flatlist]"
75 puts "test reading binary data but do not use -binary option but iso8859-1"
76 # please do not try to read binary data if your system encoding is set to
77 # utf-8. The converting from it will crash system
78 set handle2 [mysqlconnect -user root -db uni -encoding iso8859-1]
79 mysqlsel $handle2 "SELECT data from Binarytest where id=$id"
80 set nbinary [lindex [mysqlnext $handle2] 0]
81 set nfile [file tail $file].bin
82 set fhandle [open $nfile w]
83 fconfigure $fhandle -translation binary -encoding binary
84 puts "primary length [string bytelength $binary] new length [string bytelength $nbinary] - [string length $binary] [string length $nbinary]"
85 puts -nonewline $fhandle $nbinary
87 if {[catch {exec cmp $file $nfile}]} {
88 puts "binary comparing failed primary length [file size $file] new length [file size $nfile]"
90 puts "binary comparing ok"
94 puts "test with -encoding iso8859-15"
95 set handle3 [mysqlconnect -user root -db uni -encoding iso8859-15]
97 set umlaute "ÄÖ äö ß Deutsch Umlaute 26"
98 mysqlexec $handle3 "Update Binarytest set data = '$umlaute' where id=$id"
99 mysqlsel $handle3 "SELECT data from Binarytest where id=$id"
100 set umlauteOut [lindex [mysqlnext $handle3] 0]
102 puts "$umlaute $umlauteOut"
103 if {$umlaute!=$umlauteOut} {
104 puts "Umlaut Test Failed"
107 puts "Length in Mysql [mysqlsel $handle3 "SELECT LENGTH(data) from Binarytest where id=$id" -flatlist]"
111 puts "Testing finished"