Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Common ways to connect to odbc from python on windows? [closed]

Tags:

What library should I use to connect to odbc from python on windows? Is there a good alternative for pywin32 when it comes to odbc?

I'm looking for something well-documented, robust, actively maintained, etc. pyodbc looks good -- are there any others?

like image 575
user89021 Avatar asked Apr 20 '09 13:04

user89021


People also ask

Does Pyodbc close connection?

According to pyodbc documentation, connections to the SQL server are not closed by default. Some database drivers do not close connections when close() is called in order to save round-trips to the server.


2 Answers

You already suggested pyodbc, and I am going to agree with you.

It has given me the least amount of issues in my experience; I've used pymssql and adodbapi, and when those threw exceptions/created issues, I swapped out the code and replaced it with pyodbc and it either fixed the problem, or gave better error messages so I could debug faster.

It's worth mentioning that I primarily use it to connect to MSSQL Server DBs.

like image 104
Jason Coon Avatar answered Sep 17 '22 15:09

Jason Coon


I use SQLAlchemy for all python database access. I highly recommend SQLAlchemy.

SA uses pyodbc under the hood when connecting to SQL server databases. It uses other DBAPI libraries to connect to other database, for instance cx_Oracle.

A simplistic example, using SQLAlchemy like you would normally use a DBAPI module:

import sqlalchemy  engine = sqlalchemy.create_engine('sqlite:///database.db') for r in engine.execute('SELECT * FROM T'):     print(r.OneColumn, r.OtherColumn) 

But the real value of SQLAlchemy lies in its ORM and SQL expression language. Have a look, it is well worth the effort to learn to use.

like image 36
codeape Avatar answered Sep 20 '22 15:09

codeape