1

如果我在 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

在我在“WHERE”子句末尾添加“,”之前,此代码在 MAC 上不起作用。这是 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'
>>>
4

2 回答 2

0

你检查过 sql server 上的消息吗?(两台机器上的数据库连接都正常吗?)

另外,您是否尝试在调用执行之前评估字符串?就像是:

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)
于 2013-11-07T04:07:40.117 回答
0

感谢 Adam 并在这里咨询了 SQL 专家,我们成功地证明了 %s 的评估似乎在 mac 和 pc 上的工作方式不同。

所以,我重组了代码如下(类似于亚当建议的 - 但略有不同):

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!

于 2013-11-07T22:54:25.933 回答