运行 来自 Julia 的 sqlplus
running sqlplus from within Julia
我是一个非常初级的 Julia 用户,但想在我的一些项目中使用它。
我的许多项目都要求我快速连接到 Oracle 以获取其他一些数据的 ID 号。我可以通过 shell 或 tcl 等其他程序中的 运行ning sqlplus 来做到这一点,但我已经尝试了 Julia 文档中的语法,但总是会出现一个或另一个错误。
在 Tcl 中它看起来像这样
exec sqlplus -s user/pass@dbname << "
SET PAGESIZE 0 FEEDBACK OFF VERIFY OFF HEADING OFF ECHO OFF
select ID from table1 where name='abc';
exit;
"
来自 julia,我正在尝试像这样使用 运行 命令
run(`sqlplus -s user/pass@dbname << "
SET PAGESIZE 0 FEEDBACK OFF VERIFY OFF HEADING OFF ECHO OFF
select ID from table1 where name='abc';
exit;
"
`)
但我从 Julia 那里得到了各种错误,例如
Stacktrace:
[1] depwarn(::String, ::Symbol) at ./deprecated.jl:70
[2] warn_shell_special(::String) at ./shell.jl:8
[3] #shell_parse#236(::String, ::Function, ::String, ::Bool) at ./shell.jl:103
[4] (::Base.#kw##shell_parse)(::Array{Any,1}, ::Base.#shell_parse, ::String, ::Bool) at ./<missing>:0 (repeats 2 times)
[5] @cmd(::ANY) at ./process.jl:796
[6] eval(::Module, ::Any) at ./boot.jl:235
[7] eval_user_input(::Any, ::Base.REPL.REPLBackend) at ./REPL.jl:66
[8] macro expansion at ./REPL.jl:97 [inlined]
[9] (::Base.REPL.##1#2{Base.REPL.REPLBackend})() at ./event.jl:73
有人帮忙吗?
这是一个可以在我的机器上运行的函数,它还在一个变量中 returns sqlplus
命令的输出(如果需要的话)。如果不需要输出,可以使用更简单的解决方案。
sqlplus_script = """
SET PAGESIZE 0 FEEDBACK OFF VERIFY OFF HEADING OFF ECHO OFF
select ID from table1 where name='abc';
exit;
"""
sqlplus_cmd = `sqlplus -s user/pass@dbname`
# sqlplus_cmd = `cat` # used for testing
function stringpipe(cmd,instring)
inpipe = Pipe()
outpipe = Pipe()
p = spawn(pipeline(cmd,stdin=inpipe,stdout=outpipe))
write(inpipe, instring)
close(inpipe)
close(outpipe.in)
s = read(outpipe,String)
return s
end
println(stringpipe(sqlplus_cmd, sqlplus_script))
它大部分是不言自明的(顺便说一句,使用 Julia 版本 0.6,但应该可以在 0.5 上工作)。
我是一个非常初级的 Julia 用户,但想在我的一些项目中使用它。
我的许多项目都要求我快速连接到 Oracle 以获取其他一些数据的 ID 号。我可以通过 shell 或 tcl 等其他程序中的 运行ning sqlplus 来做到这一点,但我已经尝试了 Julia 文档中的语法,但总是会出现一个或另一个错误。
在 Tcl 中它看起来像这样
exec sqlplus -s user/pass@dbname << "
SET PAGESIZE 0 FEEDBACK OFF VERIFY OFF HEADING OFF ECHO OFF
select ID from table1 where name='abc';
exit;
"
来自 julia,我正在尝试像这样使用 运行 命令
run(`sqlplus -s user/pass@dbname << "
SET PAGESIZE 0 FEEDBACK OFF VERIFY OFF HEADING OFF ECHO OFF
select ID from table1 where name='abc';
exit;
"
`)
但我从 Julia 那里得到了各种错误,例如
Stacktrace:
[1] depwarn(::String, ::Symbol) at ./deprecated.jl:70
[2] warn_shell_special(::String) at ./shell.jl:8
[3] #shell_parse#236(::String, ::Function, ::String, ::Bool) at ./shell.jl:103
[4] (::Base.#kw##shell_parse)(::Array{Any,1}, ::Base.#shell_parse, ::String, ::Bool) at ./<missing>:0 (repeats 2 times)
[5] @cmd(::ANY) at ./process.jl:796
[6] eval(::Module, ::Any) at ./boot.jl:235
[7] eval_user_input(::Any, ::Base.REPL.REPLBackend) at ./REPL.jl:66
[8] macro expansion at ./REPL.jl:97 [inlined]
[9] (::Base.REPL.##1#2{Base.REPL.REPLBackend})() at ./event.jl:73
有人帮忙吗?
这是一个可以在我的机器上运行的函数,它还在一个变量中 returns sqlplus
命令的输出(如果需要的话)。如果不需要输出,可以使用更简单的解决方案。
sqlplus_script = """
SET PAGESIZE 0 FEEDBACK OFF VERIFY OFF HEADING OFF ECHO OFF
select ID from table1 where name='abc';
exit;
"""
sqlplus_cmd = `sqlplus -s user/pass@dbname`
# sqlplus_cmd = `cat` # used for testing
function stringpipe(cmd,instring)
inpipe = Pipe()
outpipe = Pipe()
p = spawn(pipeline(cmd,stdin=inpipe,stdout=outpipe))
write(inpipe, instring)
close(inpipe)
close(outpipe.in)
s = read(outpipe,String)
return s
end
println(stringpipe(sqlplus_cmd, sqlplus_script))
它大部分是不言自明的(顺便说一句,使用 Julia 版本 0.6,但应该可以在 0.5 上工作)。