SQL query within Python

328 Views Asked by At

I am trying to run a query within python and am running into errors.

I am extremely novice in the world of SQL, and one of our DBAs wrote this SQL command and it worked from their machine. However when I am converting it to Python is where I am running into issues.

import requests
from orionsdk import SwisClient

npm_server = 'SERVER'
username = 'USERNAME'
password = 'PASSWORD'

verify = False
if not verify:
    from requests.packages.urllib3.exceptions import InsecureRequestWarning
    requests.packages.urllib3.disable_warnings(InsecureRequestWarning)


swis = SwisClient(npm_server, username, password)

print("Query Test:")

query = """
SELECT NodesData.Caption as NodeName, IP_Address,
Interfaces.InterfaceName,
Interfaces.Status, Interfaces.InterfaceLastChange
 FROM Interfaces
 INNER JOIN NodesData ON Interfaces.NodeID = NodesData.NodeID
 INNER JOIN NodesCustomProperties (NOLOCK) ON NodesData.NodeID = NodesCustomProperties.NodeID
LEFT OUTER JOIN WebCommunityStrings ON WebCommunityStrings.CommunityString = NodesData.Community
 WHERE Interfaces.Status = 2 AND Interfaces.Severity > 0 AND InterfaceName = 'Tunnel201' ORDER BY NodesData.Caption, Interfaces.InterfaceIndex DESC
"""
results = swis.query(query)

for row in results['results']:
    #print("{NodeID:<5}: {DisplayName}".format(**row))
    print("{NodeID:<5}: {DisplayName}".format(**row))

Output:

C:\Users\jefhill\AppData\Local\Programs\Python\Python37-32\python.exe "C:/Users/jefhill/Desktop/Python Stuff/Projects/solarWinds/swExport.py"
Query Test:
Traceback (most recent call last):
  File "C:/Users/jefhill/Desktop/Python Stuff/Projects/solarWinds/swExport.py", line 30, in <module>
    results = swis.query(query)
  File "C:\Users\jefhill\AppData\Local\Programs\Python\Python37-32\lib\site-packages\orionsdk\swisclient.py", line 26, in query
    {'query': query, 'parameters': params}).json()
  File "C:\Users\jefhill\AppData\Local\Programs\Python\Python37-32\lib\site-packages\orionsdk\swisclient.py", line 63, in _req
    resp.raise_for_status()
  File "C:\Users\jefhill\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\models.py", line 940, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: mismatched input ')' expecting 'EQ' in Join clause for url: https://SERVER:PORT/SolarWinds/InformationService/v3/Json/Query

Process finished with exit code 1

Note: Removed some of the more private information (IE, server name, password, ect)

0

There are 0 best solutions below