I can successfully connect to SQL Server Management Studio from my jupyter notebook with this script :
from sqlalchemy import create_engine
import pyodbc
import csv
import time
import urllib
params = urllib.parse.quote_plus('''DRIVER={SQL Server Native Client 11.0};
SERVER=SV;
DATABASE=DB;
TRUSTED_CONNECTION=YES;''')
engine = create_engine("mssql+pyodbc:///?odbc_connect=%s" % params)
Just for an example, the following script perfectly works :
engine.execute("delete from table_name_X")
However, I failed to get the following script to work. For information, it works when I execute its adaptation in SQL Server Management Studio :
cde = 5
reportDate = df.loc[df.index[0],'Report Date'] # when you execute reportDate it returns 2019-11-15 00:00:00
req = "DELETE table_name_Y "
req+= "WHERE code = " + str(cde)
req+= " AND report_date = '" + str(reportDate.strftime('%Y-%m-%d')) + "'"
engine.execute(req)
According to the error message, there is a problem with the conversion of a varchar to a datetime, which created a value out of range. However, independently executed, the script str(reportDate.strftime('%Y-%m-%d')) works.
Could you please help me to understand why this previous script does not work ?
req? and add the error message too !reportDate = 2019-11-15 00:00:00produces "SyntaxError: invalid syntax"execute()separately. That being said, you'll still probably have to first convert the pandas timestamp to adatetime.