2017-07-28 56 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