"No driver name specified" writing pandas data frame into SQL Server table
I know the question has been answered for some time now and it's just a warning, but if you have transferred everything correctly and this error still occurs it's annoying.
For all those who had to struggle with it like I did, you can also enter the driver directly in the script, Pyodbc.py offers the possibility for this (row 26 - 28):
# for non-DSN connections, this *may* be used to
# hold the desired driver name
pyodbc_driver_name = 'ODBC Driver 17 for SQL Server'
The likely problem is that you have not specified the driver, so try:
engine = sqlalchemy.create_engine('mssql+pyodbc://localhost/Sandbox?trusted_connection=yes')
This is based on the warning message that you got on the top:
c:\python34\lib\site-packages\sqlalchemy\connectors\pyodbc.py:82: SAWarning: No driver name specified; this is expected by PyODBC when using DSN-less connections
"No driver name specified; "
Note that you can also use pymssql instead of pyodbc, but MS recommends the latter.
EDIT
Here is official documentation on how to connect with/without DSN (data source name):
https://github.com/mkleehammer/pyodbc/blob/master/docs/index.md#connect-to-a-database
Above information was much useful. Commenting below version of mine as consolidated which can help freshers during search.
#using library pandas and pyodbc - if not available please use pip install commands to install library based on version. Python version used here is 3.7.8
import pandas as pd
from sqlalchemy import create_engine
import pyodbc
#This query will work for sql authentication
def mssql_engine():
engine = create_engine('mssql+pyodbc://type_username:type_password@type_servername_or_localhostname/type_database_name?driver=SQL+Server+Native+Client+11.0')
return engine
#This query will for windows authentication
#Note: Uncomment below code for windows authentication
#def mssql_engine():
#engine = create_engine('mssql+pyodbc://localhostname/db_name?driver=SQL+Server+Native+Client+11.0')
#return engine
query = 'select * from table_name'
#using pandas to read from sql and passing connection string as function
df = pd.read_sql(query, mssql_engine() )
#printing result
print(df)
You need to specify both that you want to use ODBC and what ODBC driver to use.
engine = sqlalchemy.create_engine('mssql+pyodbc://localhost/Sandbox?driver=SQL+Server+Native+Client+11.0')
Trusted connections are the default, so you don't need to specify that, although it shouldn't hurt to do so.
Update:
2022-02-18: The latest ODBC driver for SQL Server seems to be "ODBC Driver 17 for SQL Server". The driver named "SQL Server" is old and should not be used.
@user1718097 gives the useful suggestion of using [x for x in pyodbc.drivers()]
to list the installed drivers.
You can also list the installed drivers with the Get-OdbcDriver
cmdlet in powershell.