-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: Errors using to_sql(), read_sql_table() when data contains percent signs #53339
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I've been doing further investigation and:
There are at least a couple of other issues that mention problems with percent signs (#11896, #34211), though they mention percent signs in column names and in SQL commands rather than table names, so I think are distinct issues. It makes me think the issue is quite likely a SQLAlchemy one - though I don't know whether pandas has control over how it uses SQLAlchemy for these operations as in #34211. I'll try updating SQLAlchemy and will post back with the outcome of that. |
My data looks like this:
If I save the same data minus the So this seems to bear some resemblance to #11896 - but that only mentions percent signs in column names, and relates to postgresql. |
Can you check if this also happens with other databases, especially (ideally) POSTGres and/or SQLite? |
Thanks, I don't have any experience with either Postgres or SQLite. I should add to my reports above - I spent quite a while longer going round in circles with the issue, and if anything became less rather than more clear on what the cause is. I wonder if it's in fact to do with holding names of valid schemas in a table column (possibly in combination with those schema names being quite idiosyncratic, and containing full stops and in some cases spaces), rather than anything to do with percent signs. But I can't say that with great confidence. Aware that's very unhelpful for trying to debug the issue - as I say, I feel like I've been going round and round with this one. |
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
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 acceptuse <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:
Where
a.b.c.d.e e
is a schema within the database (as isa.b.c.d.e
).Expected Behavior
schema
is just interpreted as a schema name, rather than database name + schema. Resulting inread_sql_table()
retrieving tableClassified roads where maintenance should be considered, %
in schemaReference.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.
The text was updated successfully, but these errors were encountered: