tags:

views:

34

answers:

1
+1  Q: 

sqlplus and Ruby

Hi,

Anybody knows how to use sqlplus in ruby? I need to do something like this: system("sqlplus username/pwd@database filename.sql"

Thx /Niklas

+1  A: 

To get sqlplus to read its input from a file you need to prefix the filename with an @ symbol. So the following would work:

system("sqlplus", "username/pwd@database", "@filename.sql")

system can either be called with a single argument (your complete command) or multiple arguments (with the arguments to your command separated out as in the above example). The documentation for Kernel#exec describes the difference (system behaves in the same way):

If exec is given a single argument, that argument is taken as a line that is subject to shell expansion before being executed. If multiple arguments are given, the second and subsequent arguments are passed as parameters to command with no shell expansion. If the first argument is a two-element array, the first element is the command to be executed, and the second argument is used as the argv[0] value, which may show up in process listings.

Note: If you want sqlplus to exit and return to your ruby program after running the SQL then make sure you include a quit statement at the end of your SQL file.

mikej