0

Using sqlplus inside python to generate SQL output. Running this via flaskplus, the formatting is lost i.e. the characters like \n,\t are not getting rendered. Tried different ways to fix this but din't help. 'print' works but 'return' doesn't work fine.

@api.route('/db-health-report')
class runSqlQuery(Resource):
    def get(self):
        session = Popen(['sqlplus', '-S', connect_string], stdin=PIPE, stdout=PIPE, stderr=PIPE)
        session.stdin.write(sql_command)
        queryResult, errorMessage = session.communicate()
        #print(queryResult, file=sys.stderr)
        #return queryResult.decode('string_escape')
        return queryResult
  • Can you show us a sample output? Is the issue that `\n` is getting _ignored_ completely, or are you seeing literal `\n` in your output? – Tim Biegeleisen Aug 21 '18 at 06:38
  • Yes, I see '\n' in the output. Example: curl http://127.0.0.1:5000/db-health-report "\n\n-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- SQL_ID/SQLTEXT with > 5 active sessions -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n\n\nno rows selected\n\n\nno rows selected\n\nRedo Generation History -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n\n\nDAY\t 00\t 01\t02 03 04 05 06\t 07\t 08\t 09\t10 11 12 13 14\t 15\t 16\t 17\t18 19 – Abhishek V Aug 21 '18 at 06:53
  • What is your database? Perhaps you need to use something like `CHR(13)` if you want a literal line break. – Tim Biegeleisen Aug 21 '18 at 06:55
  • Oracle DB Is there a way to fix this via python code itself? – Abhishek V Aug 21 '18 at 07:00
  • I don't know enough about your tech stack to say any more. In Oracle, newline is `CHR(13)` (or maybe `CHAR(13)`, I can't remember which). Maybe you could fix this in your query. – Tim Biegeleisen Aug 21 '18 at 07:02

0 Answers0