I am trying to insert data to my table in Microsoft SQL Server using a Python script.
Initially I am trying to upload an excel file, but got error messages and have tried to lesser the scale of the task. At this point I am trying to push some data trough to an already existing table in my database. In this example my servername is SERVERNAME, database is DATABASENAME and my table is TABLE. The script is:
import pyodbc
conn = pyodbc.connect('Driver={SQL Server};'
'Server=SERVERNAME;'
'Database=DATABASENAME;'
'Trusted_Connection=yes;')
cursor = conn.cursor()
cursor.execute('INSERT INTO DATABASENAME.dbo.TABLE(Name, Age, City)
VALUES ('Alan', '30', 'London');
for row in cursor:
print(row)
I get this error message:
Traceback (most recent call last):
File "<pyshell#38>", line 1, in <module>
exec(open("C:\\pandasql.py").read())
File "<string>", line 9
cursor.execute('INSERT INTO DATABASENAME.dbo.TABLE (Name, Age, City)
^
SyntaxError: EOL while scanning string literal
I want the one row with data to be seen in my database. What am I doing wrong?