tinycobol/tcltk84/tk8.4/tclodbc2.3/samples/BLOB.TCL

57 lines
1.2 KiB
Tcl

####################################################################
#
# blob.tcl
#
# Read binary data from a file, store it in the database.
# Read binary data from database and store it to a file
#
# Uses utility procedure DumpSchema in file datautil.tcl
#
# Create the db, if not yet created.
source createdb.tcl
####################################################################
#
# Delete old data
#
db "DELETE FROM Pic"
####################################################################
#
# Read binary data from an image file
#
set in [open roy.jpg]
fconfigure $in -translation binary
set data [read $in]
close $in
####################################################################
#
# Store binary data to a database table
#
db "INSERT INTO Pic (Name, Data) VALUES ('Roy',?)" {{LONGVARBINARY}} [list $data]
####################################################################
#
# Retrieve binary data
#
set data2 [lindex [lindex [db "SELECT Data FROM Pic WHERE Name = 'Roy'"] 0] 0]
####################################################################
#
# Store binary data from an image file
#
set out [open roy2.jpg w]
fconfigure $out -translation binary
puts -nonewline $out $data2
close $out