Question

I have a piece of code that works like a charm if I run the program on a mac (MBA running mavericks). If i move the code to a windows box (windows server 2008 R2 64 bit) i get an error on the SQL query itself (show below). The error indicates that there is a syntax error near the ",".

The code that I am running is below:

    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

This code did not work on the MAC until i added the "," at the end of the "WHERE" clause. This is where the windows library seems to be objecting. I have tried taking the "," out and the program runs OK but it does not evaluate the tuple properly.

I have tried various suggestions such as using """ - to no avail or change in behavior.

The run time error is show below:

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"

According to the version method the libraries in both places are on the same version. As below:

On the 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'
>>>

On Windows (two versions of python tried):

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'
>>>
Was it helpful?

Solution

Have you checked the messages on the sql server? (Is the database connection okay on both machines?)

Also, have you tried evaluating the string before calling execute? Something like:

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)

OTHER TIPS

Thanks to Adam and a consultation with and SQL guru here, we managed to prove that the evaluation of the %s seems to work differently on mac and pc.

so, i restructured the code as follows (similar to what Adam suggested - but slightly different):

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)

in other words, i built up the query by concatenating several strings. This now works on windows!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top