Description
Pandas version checks
-
I have checked that this issue has not already been reported.
-
I have confirmed this bug exists on the latest version of pandas.
-
I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
# %%
import os
import urllib
import pandas as pd
from sqlalchemy import create_engine
# %%
# CONNECT TO D/B
driver = '{ODBC Driver 17 for SQL Server}'
server = os.environ['odbc_server']
database = os.environ['odbc_database']
authentication = 'ActiveDirectoryInteractive'
username = os.environ['odbc_username']
conn = urllib.parse.quote_plus(
'DRIVER=' + driver +
';SERVER=' + server +
';DATABASE=' + database +
';UID=' + username +
';AUTHENTICATION=' + authentication + ';'
)
engine = create_engine('mssql+pyodbc:///?odbc_connect={}'.format(conn))
# %%
df = pd.DataFrame(
data=[[1, 2, 3], [4, 5, 6]],
columns=['a', 'b', 'c']
)
# %%
df.to_sql(
name='Classified roads where maintenance should be considered, %',
schema='[Reference.Infrastructure and transport.Roads.Maintenance.Source links]',
con=engine,
if_exists='replace',
index=False
)
# %%
df2 = pd.read_sql_table(
table_name='Classified roads where maintenance should be considered, %',
schema='[Reference.Infrastructure and transport.Roads.Maintenance.Source links]',
con=engine,
index_col=False
)
Issue Description
In some circumstances such as this, schema
in read_sql_table() is being interpreted as a database name + schema, rather than just a schema.
This results in a use <database>
statement - and as ODBC Driver 17 for SQL Server/Azure SQL Database doesn't accept use <database>
statements this leads to an error:
ProgrammingError: (pyodbc.ProgrammingError) ('42000', '[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]USE statement is not supported to switch between databases. Use a new connection to connect to a different database. (40508) (SQLExecDirectW)') [SQL: use [Reference.Infrastructure and transport.Roads.Maintenance]]
In this example, Reference.Infrastructure and transport.Roads.Maintenance.Source links
exists as a schema within the database. (In case of relevance, Reference.Infrastructure and transport.Roads.Maintenance
is also a schema in the database.)
I'm struggling to work out the exact trigger for the error - if it's connected to the length of the schema name/the number of full stops (periods) in it. I get the error consistently using this schema name, but am struggling to reproduce it using other schemas that follow a similar structure. E.g. the following doesn't give me an error:
df2 = pd.read_sql_table(
table_name='test',
schema='[a.b.c.d.e e]',
con=engine,
index_col=False
)
Where a.b.c.d.e e
is a schema within the database (as is a.b.c.d.e
).
Expected Behavior
schema
is just interpreted as a schema name, rather than database name + schema. Resulting in read_sql_table()
retrieving table Classified roads where maintenance should be considered, %
in schema Reference.Infrastructure and transport.Roads.Maintenance.Source links
in my example.
Installed Versions
INSTALLED VERSIONS
commit : 37ea63d
python : 3.9.10.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19045
machine : AMD64
processor : Intel64 Family 6 Model 142 Stepping 12, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United Kingdom.1252
pandas : 2.0.1
numpy : 1.22.1
pytz : 2021.3
dateutil : 2.8.2
setuptools : 58.1.0
pip : 22.1
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : 3.0.2
lxml.etree : 4.8.0
html5lib : 1.1
pymysql : None
psycopg2 : None
jinja2 : 3.0.3
IPython : 8.0.1
pandas_datareader: None
bs4 : 4.11.1
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : 3.0.9
pandas_gbq : None
pyarrow : 6.0.1
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
snappy : None
sqlalchemy : 1.4.44
tables : None
tabulate : None
xarray : None
xlrd : 2.0.1
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None
Also using pyodbc==4.0.35
I'm connecting to a database hosted in Azure SQL Database.