Reputation: 53
Does anyone know a way to connect to a SQL Server database from Python without installing a driver like ODBC? I need to do that on a customer server. I already established a connection from Python to SQL Server via pymssql, but since the project is being discontinued, I am looking for an alternative.
Is it for example possible to link a dll with odbc driver? If yes, where would I get it and how could I link it to Python?
Upvotes: 5
Views: 11877
Reputation: 406
This may be an old question, but may be relavent to many like me. I was able to get that worked by doing the below steps. If its the Windows machine, the below listed steps should work, to get python connects to MS Sql server without ODBC setting.
SQL Server Native Client 11.0
at least, as thats the last version from SQL2012 native client tool. Since 2016, its no longer shipped and rather prefer to use msoledbsql 18.xx.xx
or above.choco install SQL2012.NativeClient
. If you dont know or dont have choco, just search and download the MSI from MSFT site only.mssql+pyodbc://{YOUR-SQL-SERVER-INSTANCe}?driver=SQL+Server+Native+Client+11.0
Upvotes: 0
Reputation: 316
as of 2021-08, you can still try with pymssql(https://github.com/pymssql/pymssql) the project no longer depreciated, is active again.
if using conda, you can install from conda-forge
conda install pymssql
if import pymssql
shows error:
libiconv.so.2: cannot open shared object file
, you can also install libiconv by condaconda install libiconv
code example:
import pymssql
conn = pymssql.connect(server="127.0.0.1", port="33412", user="reader", password="passwd", database="db_name")
cursor = conn.cursor()
cursor.execute('SELECT TOP 10 * FROM table_name')
data=cursor.fetchall()
Upvotes: 7