2

I have an sql.exe which has a command line interface, takes as input an sql query and prints the results. I need to write a program in Python, which will generate the sql commands, pass them as input to this program and read the output.

For exercise, I have written a toy program in python, adder.py:

if __name__ == '__main__':
    while True:
        try:
            line = input()
            a, b = line.strip().split()
            c = int(a) + int(b)
            print(c)
        except EOFError:
            exit()
        except:
            continue

I have created a in.txt file:

10 5
7 3
1 a
2 1

I execute from cmd: -$ python adder.py < in.txt > out.txt and the out.txt is:

15
10
3

My question is, how can I stream / buffer the input and the output, to constantly communicate with the program, and not have to create and read from files again and again ?

1 Answers1

2

Use bash pipes and two different functions in your code

Try to rethink your program with this approach.

./mycode.py -gen | sqlite3 | ./mycode.py -proc
  1. Your program will accept one command-line argument that selects the behavior
    (generate the input or process the output from SQL)

  2. The character | is the pipeline operand in Bash

  3. The function that process the SQL output must read the stdin

Command-line arguments

You must read and validate the command-line arguments and choose to execute two different functions.
See here How to read/process command line arguments?

Bash pipelines

About pipeline in bash

command1 | command2

The standard output of command1 is connected via a pipe to the standard input of command2.
Example: echo "my password" | shasum -a 256

Read from the stdin

See here few examples of how to read the stdin How do you read from stdin?

Example code

See here an example code. Below an example output (tested in macOS and Ubunto using SQLite):

> ./mycode.py -gen
SELECT 0 + 0;
SELECT 1 + 2;
SELECT 2 + 4;
...

> ./mycode.py -gen | sqlite3
0
3
6
...

> # ./mycode.py -proc multiply by 10 the stdin
> ./mycode.py -gen | sqlite3 | ./mycode.py -proc
0
30
60
...
ePi272314
  • 12,557
  • 5
  • 50
  • 36
  • One more thing. The ```sql.exe``` program has the option of exporting a ```table``` into a file with a command like ```> export 'filename.xml' + tablename;```. Is it possible instead of writing to the file, to capture this output in a PIPE ? I have no access to the source code of the ```sql.exe```. –  Mar 11 '20 at 12:58
  • What SQL implementation are you running? MySQL, SQLite, PostgreSQL, ...? See the documentation, try this: `man sql.exe` or `sql.exe -h` or `sql.exe --help` or just run `sql.exe` and see how to get help in the command shell. – ePi272314 Mar 11 '20 at 14:33