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

We Are Going To Discuss About pyodbc.ProgrammingError: (‘42000’, “[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near. So lets Start this Python Article.

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

  1. How to solve pyodbc.ProgrammingError: ('42000', “[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near

    As commented by @PanagiotisKanavos, use the industry recommended best practice of SQL parameterization which goes beyond Python and SQL Server but any application layer code and any SQL-compliant database.
    Not only does this method safely escape user-submitted values, you also avoid breakage with special characters such as accent marks per your case and even quotes within the strings. Additionally, you enhance code readability, maintainability, and arguably efficiency. Even consider executemany:
    # PREPARED STATEMENT (NO DATA) query = "insert into news2 (Title, Source1, Time1) values (?, ?, ?)" # LIST OF TUPLES FOR PARAMS data = [(news.title.text, news.source.text, news.pubDate.text) for news in newslist] # EXECUTE STATEMENT AND BIND PARAMS cursor.executemany(query, data) cursor.commit()

  2. pyodbc.ProgrammingError: ('42000', “[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near

    As commented by @PanagiotisKanavos, use the industry recommended best practice of SQL parameterization which goes beyond Python and SQL Server but any application layer code and any SQL-compliant database.
    Not only does this method safely escape user-submitted values, you also avoid breakage with special characters such as accent marks per your case and even quotes within the strings. Additionally, you enhance code readability, maintainability, and arguably efficiency. Even consider executemany:
    # PREPARED STATEMENT (NO DATA) query = "insert into news2 (Title, Source1, Time1) values (?, ?, ?)" # LIST OF TUPLES FOR PARAMS data = [(news.title.text, news.source.text, news.pubDate.text) for news in newslist] # EXECUTE STATEMENT AND BIND PARAMS cursor.executemany(query, data) cursor.commit()

Solution 1

As commented by @PanagiotisKanavos, use the industry recommended best practice of SQL parameterization which goes beyond Python and SQL Server but any application layer code and any SQL-compliant database.

Not only does this method safely escape user-submitted values, you also avoid breakage with special characters such as accent marks per your case and even quotes within the strings. Additionally, you enhance code readability, maintainability, and arguably efficiency. Even consider executemany:

# PREPARED STATEMENT (NO DATA)
query = "insert into news2 (Title, Source1, Time1) values (?, ?, ?)"

# LIST OF TUPLES FOR PARAMS
data = [(news.title.text, news.source.text, news.pubDate.text) for news in newslist]

# EXECUTE STATEMENT AND BIND PARAMS
cursor.executemany(query, data)
cursor.commit()

Original Author Parfait Of This Content

Solution 2

in python3, you need to add two lines after your conn

import pyodbc as db # forgot the imports
conn = pyodbc.connect(driver=driver, server=serv, database=db,port = prt,
                  uid=usr, pwd=passwd)
conn.setdecoding(db.SQL_CHAR, encoding='latin1')
conn.setencoding('latin1')

Original Author Stella Fredo Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

ittutorial team

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment