2

I only have readonly access to a SQL Server db. How do I pass connection parameter "ApplicationIntent=ReadOnly" when using pymssql to connect to the database? If pymssql doesn't support it, is there any other python library that I can use?

0

4 Answers 4

3

The only way to configure readonly connection intent with pymssql seems to be to configure freetds on which it's based, but it did not work out for me. I've had to use pyodbc instead of pymssql

from pyodbc import connect
conn = connect(
    'DRIVER={ODBC Driver 17 for SQL Server};SERVER=' + host + ';PORT=' + port + ';DATABASE='
    + database + ';UID=' + user + ';PWD=' + password + ';ApplicationIntent=ReadOnly')
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the solution, this is working fine! The only requirement is that the ODBC driver for MSSQL is installed on the system. For example on my Windows 10 machine I installed this one.
1

Because pymssql is dependent on FreeTDS You really need to address the connection parameters to your MS-SQL server used by FreeTDS

Because ApplicationIntent is listed in the freetds user guide as an option you should be able to use it.

2 Comments

The link is not working anymore. Here is the new one.
I came to this thread trying to do same as OP and the answer is little confusing for me. Are we saying we CAN set ApplicationIntent='ReadOnly' using pymssql? If yes then how? I tried using conn=pymssql.connect('db_host_nm','db_usr_nm','db_pwd','db_nm',charset='UTF-8',port=1433,ApplicationIntent='ReadOnly') but keep getting error TypeError: connect() got an unexpected keyword argument 'ApplicationIntent' Am I missing something?
1

Latest version of pymssql has read_only property enabled.

pymssql.connect("host", "user", "pwd", "database", read_only=True)

Comments

0

pyodbc has a readonly parameter. For example:

import pyodbc
conn = pyodbc.connect(driver='{SQL Server}', host=Server, database=Database,
                  trusted_connection='yes', user='', password='', readonly = True)

2 Comments

Any reason why this was marked as not useful? Works fine for me without any issues?
Probably because the question is asking about pymssql, not pyodbc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.