tdbc::odbc resultset foreach needs an option to allow empty values
Open93c149626935bb9c7e4d7b727af6a0192b5a59a8Created 6 years agoUpdated 6 years ago
Descriptionhtml2021-04-08 05:22:15
Running on Windows 10 Pro 64-bit Build 19042
While writing a Tcl script to export MS Access tables to CSV files, I was using the tdbc::odbc package along with the system Access driver in 64-bit mode.
As mentioned in the documentation for tdbc::resultset nextdict (which foreach uses):
"If a column's value in the row is NULL, its key is omitted from the dictionary."Unfortunately I didn't see this detail until I had finished writing the bulk of my code. Because the key and value both are missing from the resultset dictionary when the value is NULL, this leaves unwanted missing fields in the CSV output when using something like this (untested concept code):
set f [open "c:/users/will/desktop/table-dump.csv" "w"]
$resultset foreach row {
foreach {key value} $row {
puts -nonewline $f "$key,"
puts $f "$value"
}
}
...
I worked around this issue by using a nextlist with while and more complicated support code, but I feel this shouldn't have been necessary. If the $resultset foreach had an -emptystringsfornulls (or some other better-named) option that would allow the key to remain in the dictionary, but with an empty string as a value, this would have allowed me to use my simple $resultset foreach code without outputing missing keys.
Log in (anonymous is fine) to add a comment.