Forums

Connecting to arbitrary SQL Server databases

Hi,

I'm building a webapp whose purpose is to provide a reporting interface on the data output of a separate piece of software. Some users of this other software have it configured to send reporting data to a database setup for this purpose.

Various databases are used but the most common are SQL Server, MySQL and Postgres.

The approach is to provide a form in which the user enters credentials for read access to their reporting database, and the web app is to use these to fetch and display the relevant information.

This works fine for MySQL and Postgres using common python libraries. My issue is with SQL Server which requires drivers that cannot be installed without root permission.

Looking at this page it seems the accepted workaround is to configure odbcinst.ini, .freetds.conf, and odbc.ini. However this requires hard-coding the connection details, which is not possible in my use case.

A potential workaround could be to run a script to update .freetds.conf, and odbc.ini as needed, though this feels uncomfortable.

Any pointers on a potential alternative would be much appreciated! I would ideally be able to connect by putting all the connection details in pyodbc.connect().

Your potential workaround is likely to cause all sorts of problems because concurrent users could easily end up using each others' connection details and end up connected to the wrong database.

You can have multiple datasource entries is odbc.ini, so you can have an entry there for each of the servers that you need to connect to.