2017-07-28 54 views
1

我是一个非常初学者的Julia用户,但想将它用于我的一些项目。从Julia内部运行sqlplus

我的许多项目都要求我快速连接Oracle以获取其他数据的ID号。我可以通过从其他程序(如shell或tcl)运行sqlplus来完成此操作,但是我尝试过Julia文档中的语法,但总是遇到一个错误或另一个错误。

在TCL一样,它看起来像这样

exec sqlplus -s user/[email protected] << " 
      SET PAGESIZE 0 FEEDBACK OFF VERIFY OFF HEADING OFF ECHO OFF 
      select ID from table1 where name='abc'; 
      exit; 
      " 

从朱莉娅,我试图使用运行命令这样

run(`sqlplus -s user/[email protected] << " 
    SET PAGESIZE 0 FEEDBACK OFF VERIFY OFF HEADING OFF ECHO OFF 
    select ID from table1 where name='abc'; 
    exit; 
    " 
    `) 

,但我得到的各种错误的茱莉亚像

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 

任何人的帮助?

+0

尝试'pipeline'命令而不是'<<' –

+0

已经试过了。我得到一个错误,如“无法产卵sqlplus -s用户/传递@ dbname。没有这样的文件或目录” – Jonjilla

+0

乔恩:对我来说,这个特定的错误意味着'sqlplus'不在你的茱莉亚环境的路径/访问。即它与你使用的特定语法无关(显然这并不意味着如果你把它放在路径中,它将立即起作用,因为语法也可能有点不对)。 –

回答

2

这是一个在我的机器上工作的函数,它还在变量中返回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/[email protected]` 
# 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)。

+0

这里是我运行这个错误。错误:MethodError:无法将Type {String}类型的对象转换为Array {UInt8,1} 类型的对象。这可能源自对构造函数Array {UInt8,1}(...)的调用, 因为类型构造函数会回退以转换方法。 – Jonjilla

+0

1.您使用的是哪个版本的Julia? 2.脚本中的MethodError在哪里? 3.是否在使用sqlplus_cmd =''''''''而不是'sqlplus'运行脚本时出错? –

+0

我正在使用0.6的发布版本。我使用sqlplus_cmd作为sqlplus,而不是'猫',但我得到这两个错误。我不确定你的问题#2。我如何知道MethodError来自哪里? – Jonjilla