Reputation: 43
I am trying to pull back all the data from my sql statement but I am only able to pull back the first row data in the first put statement. So what I am trying to do is do a for loop through the $data variable. The for loop won't run because it says that the variable is an array.
orasql crsr $sql
orafetch $crsr -datarray data -indexbyname
puts $data(customer)
foreach value $data {
puts $data(customer)
}
Upvotes: 0
Views: 487
Reputation: 137767
The problem is when you do foreach value $data
; since the data
variable is an array, you can't read it as a simple value. To print the keys and values in the array, do this:
foreach {key value} [array get data] {
puts "$key => $value"
}
The array get
command takes the name of an array variable and gets a list of keys and values from it, which can be iterated over with a two-variable foreach
. (The parray
command does a somewhat more sophisticated version of this; you could use parray data
in place of that loop above, but then you'd have no real other route into processing the row.)
You usually ought to know the names of the columns you're getting back from an SQL query so that you don't need to loop over it like that. Also, the order of columns doesn't really matter, but neither does the order of entries in the array. (The order of rows in the result set of the query matters, but orafetch
only gets one at a time.)
Upvotes: 1