Skip to content

Commit d5798e8

Browse files
committed
FIX: execute() works on a cursor.
1 parent 165b604 commit d5798e8

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

pandas/io/sql.py

+15-6
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,10 @@ def execute(sql, con, cur=None, params=None, flavor='sqlite'):
9898
-------
9999
Results Iterable
100100
"""
101-
pandas_sql = pandasSQL_builder(con, flavor=flavor)
101+
if cur is None:
102+
pandas_sql = pandasSQL_builder(con, flavor=flavor)
103+
else:
104+
pandas_sql = pandasSQL_builder(cur, flavor=flavor, is_cursor=True)
102105
args = _convert_params(sql, params)
103106
return pandas_sql.execute(*args)
104107

@@ -473,11 +476,13 @@ def has_table(table_name, con, flavor='sqlite'):
473476
table_exists = has_table
474477

475478

476-
def pandasSQL_builder(con, flavor=None, meta=None):
479+
def pandasSQL_builder(con, flavor=None, meta=None, is_cursor=False):
477480
"""
478481
Convenience function to return the correct PandasSQL subclass based on the
479482
provided parameters
480483
"""
484+
# When support for DBAPI connections is removed,
485+
# is_cursor should not be necessary.
481486
try:
482487
import sqlalchemy
483488

@@ -491,14 +496,14 @@ def pandasSQL_builder(con, flavor=None, meta=None):
491496
"PandasSQL must be created with an SQLAlchemy engine "
492497
"or a DBAPI2 connection and SQL flavor")
493498
else:
494-
return PandasSQLLegacy(con, flavor)
499+
return PandasSQLLegacy(con, flavor, is_cursor=is_cursor)
495500

496501
except ImportError:
497502
warnings.warn("SQLAlchemy not installed, using legacy mode")
498503
if flavor is None:
499504
raise SQLAlchemyRequired
500505
else:
501-
return PandasSQLLegacy(con, flavor)
506+
return PandasSQLLegacy(con, flavor, is_cursor=is_cursor)
502507

503508

504509
class PandasSQLTable(PandasObject):
@@ -983,16 +988,20 @@ def _sql_type_name(self, dtype):
983988

984989
class PandasSQLLegacy(PandasSQL):
985990

986-
def __init__(self, con, flavor):
991+
def __init__(self, con, flavor, is_cursor=False):
992+
self.is_cursor = is_cursor
987993
self.con = con
988994
if flavor not in ['sqlite', 'mysql']:
989995
raise NotImplementedError
990996
else:
991997
self.flavor = flavor
992998

993999
def execute(self, *args, **kwargs):
994-
try:
1000+
if self.is_cursor:
1001+
cur = self.con
1002+
else:
9951003
cur = self.con.cursor()
1004+
try:
9961005
if kwargs:
9971006
cur.execute(*args, **kwargs)
9981007
else:

0 commit comments

Comments
 (0)