首页 > 解决方案 > 使用 pandas 读取“只读”SQL

问题描述

我试图使用以下代码将 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 数据框?

标签: pythonsql-serverpandas

解决方案


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


推荐阅读