使用熊猫读取“只读” SQL

时间:2019-10-21 19:50:31

标签: python sql-server pandas

我正尝试使用以下代码将sql表读入pandas数据框中:

import pandas as pd
import pyodbc as pb 

conn = pb.connect(
    'DRIVER={SQL Server Native Client 11.0};'
    'SERVER=server_name;'
    'DATABASE=db;'
    'Trusted_Connection=yes'
    )

sql_builder = pd.read_sql_query(
    'SELECT * from schema.table',conn
)

df=pd.DataFrame(sql_builder)

它具有以下错误:

Traceback (most recent call last):
  File "c:/Users/user/Desktop/SQL.py", line 6, in <module>
    'DRIVER={SQL Server Native Client 11.0};'
pyodbc.ProgrammingError: ('42000', "[42000] [Microsoft][SQL Server Native Client 11.0][SQL Server]The target database ('db') is in an availability group and is
currently accessible for connections when the application intent is set to read only. For more information about application intent, see SQL Server Books Online. (978) (SQLDriverConnect); [42000] [Microsoft][SQL Server Native Client 11.0][SQL Server]The target database ('dbDepot') is in an availability group and is currently accessible for connections when the application intent is set to read only. For more information about application intent, see SQL Server Books Online. (978)")

我的问题是,如何将只读表读入pandas数据框?

1 个答案:

答案 0 :(得分:0)

尝试将连接的自动提交设置为True,以防止在连接上发送开始事务。