Connect Python to SQL Server without ODBC

xmjla07d  于 2023-03-22  发布在  Python
关注(0)|答案(2)|浏览(106)

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?

noj0wjuj

noj0wjuj1#

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 conda conda 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()
6vl6ewon

6vl6ewon2#

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.

  1. Need to install/setup 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.

  2. You can download this from internet by searching for this or, i used Choco to install this for me. choco install SQL2012.NativeClient . If you dont know or dont have choco, just search and download the MSI from MSFT site only.

  3. Ensure you see a version in the ODBC drivers tab like this

  4. Ensure you have your connection string refers that exact driver name like, mssql+pyodbc://{YOUR-SQL-SERVER-INSTANCe}?driver=SQL+Server+Native+Client+11.0

  5. Certainly your python needs pyodbc library and sqlalchemy to use this.

相关问题