0

I tried to use this code through python to run sql-server script

    import pyodbc
# 'driver = 'SQL Server'
host = '**DESKTOP-A2HRFI1\SQLSERVER**'
db1 = 'master'
tcon = 'yes'
# uname = 'jnichol3'
# pword = '**my-password**'


cnxn = pyodbc.connect(driver='{SQL Server}', host, db1, trusted_Connection=tcon)
cursor = cnxn.cursor()
cursor.execute("SELECT TOP (1000) [Student_name],[Serial_no]FROM [master].[dbo].[StudentTable]")
rows = cursor.fetchall()
for row in rows:
    print row

I have this error, please help me to solve it

PS C:\Users\Admin\OneDrive\Documents\Python Projects> & C:/Users/Admin/AppData/Local/Programs/Python/Python39/python.exe

"c:/Users/Admin/OneDrive/Documents/Python Projects/Run_SQL.py" File "c:\Users\Admin\OneDrive\Documents\Python Projects\Run_SQL.py", line 10 cnxn = pyodbc.connect(driver='{SQL Server}', host, db1, trusted_Connection=tcon) ^ SyntaxError: positional argument follows keyword argument

Thom A
  • 88,727
  • 11
  • 45
  • 75
  • @HoneyBadger that's `tempdb` not `master`. Though creating objects in `master` is still ill-advised unless you plan for them to be system objects. – Thom A Nov 27 '20 at 10:18
  • Also, having a `TOP` without an `ORDER BY` and using 3 part naming to reference objects in the database you are connected to both have "code smells" to them. – Thom A Nov 27 '20 at 10:21
  • Does this answer your question? [Why can't non-default arguments follow default arguments?](https://stackoverflow.com/questions/16932825/why-cant-non-default-arguments-follow-default-arguments). That I've got from [positional argument follows keyword argument](https://stackoverflow.com/questions/42163846/positional-argument-follows-keyword-argument) – astentx Nov 27 '20 at 10:23
  • Anyway it is pure python error that has nothing with SQL. Any `syntax error` should not be considered to have any DBMS tags – astentx Nov 27 '20 at 10:24

0 Answers0