如果我在mac上运行程序(MBA运行的小牛队),我有一段像魅力一样的代码。如果我将代码移动到Windows框(Windows Server 2008 R2 64位),我会在SQL查询本身上收到错误(如下所示)。该错误表示“,”附近存在语法错误。
我正在运行的代码如下:
try:
cur.execute("SELECT * FROM dbo.IPAM_Node as A \
FULL OUTER JOIN IPAM_NodeAttrData as B ON A.IPNodeId = B.IPNodeId \
FULL OUTER JOIN IPAM_Group as C on A.SubnetId = C.GroupId \
FULL OUTER JOIN IPAM_GroupAttrData as D on C.GroupId = D.GroupId \
WHERE IPAddress IN (%s);",(Hosts_as_Tuples,))
allrows = cur.fetchall()
print 'allrows:', allrows
此代码在MAC上不起作用,直到我在“WHERE”子句的末尾添加了“,”。这是Windows库似乎反对的地方。我已经尝试取出“,”并且程序运行正常,但它没有正确评估元组。
我尝试了各种建议,例如使用“”“ - 无效或改变行为。
运行时错误如下所示:
C:\SFTP_Root\v1.0.1.d\Model>[07/Nov/2013:12:25:27] ENGINE Listening for SIGTERM.
[07/Nov/2013:12:25:27] ENGINE Bus STARTING
[07/Nov/2013:12:25:27] ENGINE Set handler for console events.
CherryPy Checker:
The Application mounted at '' has an empty config.
[07/Nov/2013:12:25:27] ENGINE Started monitor thread 'Autoreloader'.
[07/Nov/2013:12:25:27] ENGINE Started monitor thread '_TimeoutMonitor'.
[07/Nov/2013:12:25:27] ENGINE Serving on 10.188.49.151:4444
[07/Nov/2013:12:25:27] ENGINE Bus STARTED
it is NOT a list
Host List: ['10.188.49.0', '10.188.49.1', '10.188.49.2', '10.188.49.3']
**DATABASE ERROR: (102, "Incorrect syntax near ','.DB-Lib error message 102, sever
ity 15:\nGeneral SQL Server error: Check messages from the SQL Server\n")**
144.131.52.107 - - [07/Nov/2013:12:25:38] "GET /ip/informationservice/?ipaddress
=10.188.49.0/30 HTTP/1.1" 200 346 "" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.
9; rv:25.0) Gecko/20100101 Firefox/25.0"
根据版本方法,两个地方的库都在同一个版本上。如下:
在Mac上:
isp-pc:site-packages matingara$ python
Python 2.7.5 (v2.7.5:ab05e7dd2788, May 13 2013, 13:18:45)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pymssql
>>> pymssql.__version__
'2.0.0'
>>>
在Windows上(尝试了两个版本的python):
C:\Python27>python.exe
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pymssql
>>> pymssql.__version__
'2.0.0'
>>>
C:\Python27>python.exe
Python 2.7.5 (default, May 15 2013, 22:44:16) [MSC v.1500 64 bit (AMD64)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pymssql
>>> pymssql.__version__
'2.0.1'
>>>
答案 0 :(得分:0)
你检查过sql server上的消息了吗? (这两台机器上的数据库连接是否正常?)
另外,您是否尝试在调用execute之前评估字符串?类似的东西:
queryString = "SELECT * FROM dbo.IPAM_Node as A \
FULL OUTER JOIN IPAM_NodeAttrData as B ON A.IPNodeId = B.IPNodeId \
FULL OUTER JOIN IPAM_Group as C on A.SubnetId = C.GroupId \
FULL OUTER JOIN IPAM_GroupAttrData as D on C.GroupId = D.GroupId \
WHERE IPAddress IN (%s);" % (Hosts_as_Tuples,)
print queryString
cur.execute(queryString)
答案 1 :(得分:0)
感谢Adam和这里的SQL大师的咨询,我们设法证明%s的评估在mac和pc上似乎有所不同。
所以,我重新编写了如下代码(类似于Adam建议 - 但略有不同):
try:
sqlQuery = "SELECT * FROM dbo.IPAM_Node as A \
FULL OUTER JOIN IPAM_NodeAttrData as B ON A.IPNodeId = B.IPNodeId \
FULL OUTER JOIN IPAM_Group as C on A.SubnetId = C.GroupId \
FULL OUTER JOIN IPAM_GroupAttrData as D on C.GroupId = D.GroupId \
WHERE IPAddress IN " + str(Hosts_as_Tuples) + ";"
cur.execute(sqlQuery)
换句话说,我通过连接几个字符串来构建查询。这现在适用于Windows!