Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot insert datetime.date object into Sql Server #658

Closed
FrankMillman opened this issue Dec 14, 2019 · 2 comments
Closed

Cannot insert datetime.date object into Sql Server #658

FrankMillman opened this issue Dec 14, 2019 · 2 comments

Comments

@FrankMillman
Copy link

Python: 3.7.2
pyodbc: 4.0.27
OS: Windows10 64 bit
DB: Sql Server
Driver: I installed ODBC Driver 17 for SQL Server - don't know how to check if it is using it
Connection string: driver='sql server', server=r'localhost\sqlexpress', database={db}, uid={uid}, pwd={pwd}, trusted_connection=True

As abbreviations in the following, dt refers to a python datetime.date object, dtm refers to a python datetime.datetime object.

If I create a column of type DATE and insert a dt, I get this error -

pyodbc.Error: ('HYC00', '[HYC00] [Microsoft][ODBC SQL Server Driver]Optional feature not implemented (0) (SQLBindParameter)')

If I create a column of type DATE and insert a dtm, it accepts it, but when I retrieve it I get a string 'yyyy-mm-dd'.

If I create a column of type DATETIME and insert a dt, I get the same error as above.

If I create a column of type DATETIME and insert a dtm, it accepts it and returns a dtm.

Ideally I would like to create a column of type DATE, insert a dt, and get back a dt.

Am I doing something wrong?

@gordthompson
Copy link
Collaborator

DRIVER=SQL Server in your connection string will use the very old (circa SQL Server 2000) ODBC driver that ships with Windows. If you want to use the newer driver that you installed then put

DRIVER=ODBC Driver 17 for SQL Server

in your connection string.

don't know how to check if it is using it

After you've established your connection (cnxn = pyodbc.connect(...)) you can do

print(f'driver: {cnxn.getinfo(pyodbc.SQL_DRIVER_NAME)}, version: {cnxn.getinfo(pyodbc.SQL_DRIVER_VER)}')
# driver: msodbcsql17.dll, version 17.04.0001

With the older driver you would see driver: SQLSRV32.DLL, version: 06.03.9600.

@FrankMillman
Copy link
Author

Thank you so much - it works perfectly.

It took me two days to get it working. Eventually I figured out that the connection string parameter 'trusted_connection' now requires 'Yes' instead of True!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants