From 8d27dbce7f9ff7bb5b80bb5f9584f7c6580a5cc6 Mon Sep 17 00:00:00 2001 From: Fangchen Li Date: Mon, 12 Jul 2021 10:50:28 -0500 Subject: [PATCH 1/2] CLN: remove deprecated cursor param --- pandas/io/sql.py | 24 ++++++------------------ pandas/tests/io/test_sql.py | 12 ++++++------ 2 files changed, 12 insertions(+), 24 deletions(-) diff --git a/pandas/io/sql.py b/pandas/io/sql.py index 1a8012df5cb46..df9c7e28bff69 100644 --- a/pandas/io/sql.py +++ b/pandas/io/sql.py @@ -182,7 +182,7 @@ def _wrap_result( return frame -def execute(sql, con, cur=None, params=None): +def execute(sql, con, params=None): """ Execute the given SQL query using the provided connection object. @@ -194,7 +194,6 @@ def execute(sql, con, cur=None, params=None): Using SQLAlchemy makes it possible to use any DB supported by the library. If a DBAPI2 object, only sqlite3 is supported. - cur : deprecated, cursor is obtained from connection, default: None params : list or tuple, optional, default: None List of parameters to pass to execute method. @@ -202,10 +201,7 @@ def execute(sql, con, cur=None, params=None): ------- Results Iterable """ - if cur is None: - pandas_sql = pandasSQL_builder(con) - else: - pandas_sql = pandasSQL_builder(cur, is_cursor=True) + pandas_sql = pandasSQL_builder(con) args = _convert_params(sql, params) return pandas_sql.execute(*args) @@ -774,22 +770,18 @@ def _engine_builder(con): return con -def pandasSQL_builder( - con, schema: str | None = None, meta=None, is_cursor: bool = False -): +def pandasSQL_builder(con, schema: str | None = None, meta=None): """ Convenience function to return the correct PandasSQL subclass based on the provided parameters. """ - # When support for DBAPI connections is removed, - # is_cursor should not be necessary. con = _engine_builder(con) if _is_sqlalchemy_connectable(con): return SQLDatabase(con, schema=schema, meta=meta) elif isinstance(con, str): raise ImportError("Using URI string without sqlalchemy installed.") else: - return SQLiteDatabase(con, is_cursor=is_cursor) + return SQLiteDatabase(con) class SQLTable(PandasObject): @@ -2031,8 +2023,7 @@ class SQLiteDatabase(PandasSQL): """ - def __init__(self, con, is_cursor: bool = False): - self.is_cursor = is_cursor + def __init__(self, con): self.con = con @contextmanager @@ -2048,10 +2039,7 @@ def run_transaction(self): cur.close() def execute(self, *args, **kwargs): - if self.is_cursor: - cur = self.con - else: - cur = self.con.cursor() + cur = self.con.cursor() try: cur.execute(*args, **kwargs) return cur diff --git a/pandas/tests/io/test_sql.py b/pandas/tests/io/test_sql.py index 97b3a82440ee6..347c9d41893b4 100644 --- a/pandas/tests/io/test_sql.py +++ b/pandas/tests/io/test_sql.py @@ -2615,9 +2615,9 @@ def format_query(sql, *args): return sql % tuple(processed_args) -def tquery(query, con=None, cur=None): +def tquery(query, con=None): """Replace removed sql.tquery function""" - res = sql.execute(query, con=con, cur=cur).fetchall() + res = sql.execute(query, con=con).fetchall() if res is None: return None else: @@ -2652,9 +2652,9 @@ def test_write_row_by_row(self): cur = self.conn.cursor() ins = "INSERT INTO test VALUES (%s, %s, %s, %s)" - for idx, row in frame.iterrows(): + for _, row in frame.iterrows(): fmt_sql = format_query(ins, *row) - tquery(fmt_sql, cur=cur) + tquery(fmt_sql, con=self.conn) self.conn.commit() @@ -2912,9 +2912,9 @@ def test_write_row_by_row(self): cur.execute(drop_sql) cur.execute(create_sql) ins = "INSERT INTO test VALUES (%s, %s, %s, %s)" - for idx, row in frame.iterrows(): + for _, row in frame.iterrows(): fmt_sql = format_query(ins, *row) - tquery(fmt_sql, cur=cur) + tquery(fmt_sql, con=self.conn) self.conn.commit() From 425f0a9a52003c1573441eb927118b2c860909d7 Mon Sep 17 00:00:00 2001 From: Fangchen Li Date: Mon, 12 Jul 2021 11:01:53 -0500 Subject: [PATCH 2/2] clean --- pandas/tests/io/test_sql.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/pandas/tests/io/test_sql.py b/pandas/tests/io/test_sql.py index 347c9d41893b4..9320bf385ce0a 100644 --- a/pandas/tests/io/test_sql.py +++ b/pandas/tests/io/test_sql.py @@ -2649,8 +2649,6 @@ def test_write_row_by_row(self): cur = self.conn.cursor() cur.execute(create_sql) - cur = self.conn.cursor() - ins = "INSERT INTO test VALUES (%s, %s, %s, %s)" for _, row in frame.iterrows(): fmt_sql = format_query(ins, *row)