我的 orafetch 语句有什么问题?
What is wrong with my orafetch statement?
出于某种原因,我的 orafetch 语句返回错误:
orafetch: handle 0 not open while executing
有人可以指出我的陈述哪里有问题吗?
if {[catch {
set crsr [oraopen $DB_LOGON_HANDLE]
} result] } {
puts "Error $result while creating db handles"
exit 1
}
set name "select names from customers where name = 'Tim Kyle'"
set name_query [orasql $crsr $name ]
set get_name [orafetch $name_query -dataarray -indexbyname]
puts $get_name
这里有示例代码 on the Tcler's Wiki,我在此处重现:
# For error reporting:
set program [file tail $argv0]
# Package interface:
package require Oratcl
# Connect to the $env(TWO_TASK) database as USER with PASSWORD:
if [catch {oralogon "USER/PASSWORD"} ora_logon] {
puts stderr "$program: $ora_logon"
exit 1
}
if [catch {oraopen $ora_logon} ora_statement] {
oralogoff $ora_logon
puts stderr "$program: $ora_statement"
exit 1
}
#if [catch {oraconfig $ora_statement fetchrows 1024} ora_error] {
# puts stderr "$program: $ora_error"
#}
# Execute SQL statement:
set sql "SELECT column_1, ... column_N FROM ... WHERE ..."
# Note that for Oratcl 4.x, the $oramsg references have to change to
# [oramsg $ora_statement rc]
if [catch {orasql $ora_statement $sql} ora_error] {
puts stderr "$program: $ora_error"
} elseif {$oramsg(rc) != 0} {
puts stderr "$program: $oramsg(errortxt)"
} else {
# Process each row with column_I bound to var_I:
while {$oramsg(rc) == 0} {
if [catch {orafetch $ora_statement \
{... $var_1 ... $var_N ...} \
'@' var_1 1 ... var_N N} \
ora_error] {
puts stderr "$program: $ora_error"
break
} elseif {$oramsg(rc) == 1403} {
break
} elseif {$oramsg(rc) != 0} {
puts stderr "$program: $oramsg(errortxt)"
break
}
}
}
# Disconnect from the $env(TWO_TASK) database:
if [catch {oraclose $ora_statement ora_error] {
puts stderr "$program: $ora_error"
}
if [catch {oralogoff $ora_logon ora_error] {
puts stderr "$program: $ora_error"
}
这一切对我来说非常复杂!特别是与 oramsg
相关的所有内容(这似乎是您的代码中缺少的内容)和 orafetch
绑定。
您可以考虑使用 tdbc::oracle
as a wrapper, which follows the standard TDBC 模型(该模型是根据 许多 数据库接口中的最佳实践经验开发的)。
package require tdbc::oracle
tdbc::oratcl connection create db USER/PASSWORD@SID
db foreach rec { select names from customers where name = 'Tim Kyle' } {
puts $rec
}
# Optionally shut everything down at the end with:
# db destroy
出于某种原因,我的 orafetch 语句返回错误:
orafetch: handle 0 not open while executing
有人可以指出我的陈述哪里有问题吗?
if {[catch {
set crsr [oraopen $DB_LOGON_HANDLE]
} result] } {
puts "Error $result while creating db handles"
exit 1
}
set name "select names from customers where name = 'Tim Kyle'"
set name_query [orasql $crsr $name ]
set get_name [orafetch $name_query -dataarray -indexbyname]
puts $get_name
这里有示例代码 on the Tcler's Wiki,我在此处重现:
# For error reporting:
set program [file tail $argv0]
# Package interface:
package require Oratcl
# Connect to the $env(TWO_TASK) database as USER with PASSWORD:
if [catch {oralogon "USER/PASSWORD"} ora_logon] {
puts stderr "$program: $ora_logon"
exit 1
}
if [catch {oraopen $ora_logon} ora_statement] {
oralogoff $ora_logon
puts stderr "$program: $ora_statement"
exit 1
}
#if [catch {oraconfig $ora_statement fetchrows 1024} ora_error] {
# puts stderr "$program: $ora_error"
#}
# Execute SQL statement:
set sql "SELECT column_1, ... column_N FROM ... WHERE ..."
# Note that for Oratcl 4.x, the $oramsg references have to change to
# [oramsg $ora_statement rc]
if [catch {orasql $ora_statement $sql} ora_error] {
puts stderr "$program: $ora_error"
} elseif {$oramsg(rc) != 0} {
puts stderr "$program: $oramsg(errortxt)"
} else {
# Process each row with column_I bound to var_I:
while {$oramsg(rc) == 0} {
if [catch {orafetch $ora_statement \
{... $var_1 ... $var_N ...} \
'@' var_1 1 ... var_N N} \
ora_error] {
puts stderr "$program: $ora_error"
break
} elseif {$oramsg(rc) == 1403} {
break
} elseif {$oramsg(rc) != 0} {
puts stderr "$program: $oramsg(errortxt)"
break
}
}
}
# Disconnect from the $env(TWO_TASK) database:
if [catch {oraclose $ora_statement ora_error] {
puts stderr "$program: $ora_error"
}
if [catch {oralogoff $ora_logon ora_error] {
puts stderr "$program: $ora_error"
}
这一切对我来说非常复杂!特别是与 oramsg
相关的所有内容(这似乎是您的代码中缺少的内容)和 orafetch
绑定。
您可以考虑使用 tdbc::oracle
as a wrapper, which follows the standard TDBC 模型(该模型是根据 许多 数据库接口中的最佳实践经验开发的)。
package require tdbc::oracle
tdbc::oratcl connection create db USER/PASSWORD@SID
db foreach rec { select names from customers where name = 'Tim Kyle' } {
puts $rec
}
# Optionally shut everything down at the end with:
# db destroy