1

I am using Pyodbc, and am connected to a database. I can easily pull out data where I am just using the SELECT and FROM statements.

However when I try to use a WHERE statement it is throwing syntax errors:

This is the code:

import pyodbc

conn = pyodbc.connect('DSN=QueryBuilder')
cursor = conn.cursor()
cursor.execute('SELECT * FROM dbo.Grantinformation WHERE HoldingOrganisationName = 'university of edinburgh'')

I get this error:

SyntaxError: invalid syntax

If i run:

SELECT * 
FROM dbo.Grantinformation
WHERE HoldingOrganisationName = 'university of edinburgh'

in SQL Server Management Studio the SQL runs fine, so it is obviously something that I am doing wrong with pyodbc?

Many thanks

1 Answer 1

6

No, this is a simple Python syntax error. You have single quotes inside your sql string, so you need to use double quotes around the string itself:

cursor.execute("SELECT * FROM dbo.Grantinformation WHERE HoldingOrganisationName = 'university of edinburgh'")
Sign up to request clarification or add additional context in comments.

1 Comment

Whoops! Thank you Daniel!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.