Skip to content

Commit 0ffb7e9

Browse files
erichxchenHanxin Chen
and
Hanxin Chen
authored
DOC: Add docs for read_sql to avoid sql injection (#56546)
* add docs for read_sql to avoid sql injection * resolve formatting * re-word the docs * formatting * refactor the example due to the length constraints * remove reference directing users to use params * changed to the general warning * add the example for using params --------- Co-authored-by: Hanxin Chen <[email protected]>
1 parent c7cc22a commit 0ffb7e9

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

pandas/io/sql.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,14 @@ def read_sql(
652652
read_sql_table : Read SQL database table into a DataFrame.
653653
read_sql_query : Read SQL query into a DataFrame.
654654
655+
Notes
656+
-----
657+
``pandas`` does not attempt to sanitize SQL statements;
658+
instead it simply forwards the statement you are executing
659+
to the underlying driver, which may or may not sanitize from there.
660+
Please refer to the underlying driver documentation for any details.
661+
Generally, be wary when accepting statements from arbitrary sources.
662+
655663
Examples
656664
--------
657665
Read data from SQL via either a SQL query or a SQL tablename.
@@ -672,6 +680,14 @@ def read_sql(
672680
673681
>>> pd.read_sql('test_data', 'postgres:///db_name') # doctest:+SKIP
674682
683+
For parameterized query, using ``params`` is recommended over string interpolation.
684+
685+
>>> from sqlalchemy import text
686+
>>> sql = text('SELECT int_column, date_column FROM test_data WHERE int_column=:int_val')
687+
>>> pd.read_sql(sql, conn, params={'int_val': 1}) # doctest:+SKIP
688+
int_column date_column
689+
0 1 12/11/10
690+
675691
Apply date parsing to columns through the ``parse_dates`` argument
676692
The ``parse_dates`` argument calls ``pd.to_datetime`` on the provided columns.
677693
Custom argument values for applying ``pd.to_datetime`` on a column are specified
@@ -694,7 +710,7 @@ def read_sql(
694710
int_column
695711
0 0
696712
1 1
697-
"""
713+
""" # noqa: E501
698714

699715
check_dtype_backend(dtype_backend)
700716
if dtype_backend is lib.no_default:

0 commit comments

Comments
 (0)