Python中的SQL查询
我试图在python中运行查询,但遇到了错误 我对SQL非常熟悉,我们的一位DBA编写了这个SQL命令,它在他们的机器上运行。然而,当我将其转换为Python时,我遇到了一些问题Python中的SQL查询,python,sql,python-3.x,solarwinds-orion,Python,Sql,Python 3.x,Solarwinds Orion,我试图在python中运行查询,但遇到了错误 我对SQL非常熟悉,我们的一位DBA编写了这个SQL命令,它在他们的机器上运行。然而,当我将其转换为Python时,我遇到了一些问题 import requests from orionsdk import SwisClient npm_server = 'SERVER' username = 'USERNAME' password = 'PASSWORD' verify = False if not verify: from reque
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))
输出:
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
注意:删除了一些更私密的信息,例如服务器名称、密码等看起来它在抱怨查询中的NOLOCK。我不是SQL专家,但在谷歌搜索之后,它似乎只在微软的SQL Server中使用。我不确定您连接的是哪个数据库,它是由您使用的Python库处理的,但我猜它不是SQL Server。@RobinZigmond id假设NOLOCK不在我的库中-现在去谷歌看看是否有其他使用同一个库的人遇到过这个问题。您在Orion SWQL studio上尝试过您的查询吗?