Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7989863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:51:42+00:00 2026-06-04T12:51:42+00:00

I am trying to get information from an SQL database using python I was

  • 0

I am trying to get information from an SQL database using python

I was able to connect and retrieve data when the SQL statement was simple such as

cursor.execute("SELECT * FROM Client WHERE UsesTimesheet = 1 ORDER BY ClientName")

However, when I move to a more complex statement I get the error shown below

Traceback (most recent call last):
 File "F:\Python\Test - AutoCad.py", line 30, in <module>
where jobnum = 1205992")
File "C:\Python26\ArcGIS10.0\lib\site-packages\pymssql.py", line 196, in execute
raise OperationalError, e[0]
OperationalError: SQL Server message 102, severity 15, state 1, line 1:
Incorrect syntax near 'jobnum'.

This statement works when I use Microsoft SQL 2008 Client but not in Python.

What am I doing incorrectly? For complex statements should I be using SQLAlchemy?

Current Code Below

import pymssql
import _mssql
import sys

# Connect to db using Windows Integrated Authentication.
conn = _mssql.connect(server='000.000.0.0', database='Mydb', trusted=True)
conn = pymssql.connect(host='000.000.0.0', database='Mydb', trusted=True)

# prepare a cursor object using cursor() method
cursor = conn.cursor()

cursor.execute("SELECT PJI.*, PJO.*, \
        CST.ABCGS \
FROM  dbo.Traverse AS TRE \
              LEFT OUTER JOIN dbo.TraversePreEntry AS TPE \
                    ON TRE.JobNum = dbo.GetJobNumberFromGroupId(TPE.GroupId)\
              LEFT OUTER JOIN AutoCADProjectInformation AS PJI\
                    ON TRE.JobNum = PJI.JobNumber \
              LEFT OUTER JOIN CalculationStorageReplacement AS CST \
                    ON CST.ProjectNumber = dbo.GetJobNumberFromGroupId(TPE.GroupId)\
              LEFT OUTER JOIN dbo.TraverseElevations AS TEV\
                  ON TRE.TraverseId = TEV.TraverseId\
              LEFT OUTER JOIN VGSDB.dbo.ProjectOffice PJO\
                    ON PJI.PjbId = PJO.PjbId\
where jobnum = 1205992")

# Fetch rows
data = cursor.fetchall()

print "Info : %s " % str(data)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T12:51:43+00:00Added an answer on June 4, 2026 at 12:51 pm

    Your python string is being joined together without newlines, thus there is no space before the where keyword. Better use triple-quoted strings when working with multi-line string literals:

    cursor.execute("""\
    SELECT PJI.*, PJO.*, 
            CST.ABCGS 
    FROM  dbo.Traverse AS TRE 
                  LEFT OUTER JOIN dbo.TraversePreEntry AS TPE 
                        ON TRE.JobNum = dbo.GetJobNumberFromGroupId(TPE.GroupId)
                  LEFT OUTER JOIN AutoCADProjectInformation AS PJI
                        ON TRE.JobNum = PJI.JobNumber
                  LEFT OUTER JOIN CalculationStorageReplacement AS CST
                        ON CST.ProjectNumber = dbo.GetJobNumberFromGroupId(TPE.GroupId)
                  LEFT OUTER JOIN dbo.TraverseElevations AS TEV
                      ON TRE.TraverseId = TEV.TraverseId
                  LEFT OUTER JOIN VGSDB.dbo.ProjectOffice PJO
                        ON PJI.PjbId = PJO.PjbId
    where jobnum = 1205992""")
    

    Triple-quoted strings maintain newlines:

    >>> "one\
    ... two"
    "onetwo"
    >>> """one
    ... two"""
    "one\ntwo"
    

    If this is a one-of you do not necessarily need to use SQLAlchemy, but as your project grows you’ll find that that library offers many advantages, including making conditional logic much easier (adding further WHERE clauses based on if/then branches, etc).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to use python's telnetlib module to get information from a remote
Using ASP.NET MVC when trying to get the information stored on my Session[objectName] from
I'm trying to import data from a Progress database into a MS SQL 2005
I'm using an SQL query to gather information from a database and C# +
I'm trying to get information from a cookie into Alfresco Share. I have a
I am trying to get disk information from fdisk -l output on linux. fdisk
Here is my basic situation. I'm trying to use NHibernate to get information from
I'm trying to get two pieces of information from the following cursor: 1. Subdomain
Hi I am trying to get information on an Oracle package directly from PHP
I am trying to get information on the version of Windows installed from WMI.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.