Skip to content
Advertisement

Incorrect syntax while accessing SQL table using Python

I’m trying to access a SQL Express database using Python, but I keep getting an incorrect syntax error. I’ve tried a few variations based of other examples I’ve seen, but nothing’s working.

import pyodbc

conn = pyodbc.connect(Trusted_connection='yes',
                      driver='{SQL Server}',
                      server='VACE1RCARCHIVE',
                      database='TestDB')

cursor = conn.cursor()
cursor.execute("select * from 100000MTL1")

for row in cursor:
    print(row)

The error is

pyodbc.ProgrammingError: (‘42000’, “[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near ‘100000’. (102) (SQLExecDirectW)”)

I’ve tried changing the database and the name of the database, but I’m not sure if that is where the error is exactly, so I’m totally stuck now.

Advertisement

Answer

Escape your table name like this

select * from [100000MTL1] 

since table names usually don’t start with numbers.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement