Skip to content

Commit ff95ac3

Browse files
danielballanjorisvandenbossche
authored andcommitted
FIX: execute() works on a cursor.
1 parent 165b604 commit ff95ac3

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

pandas/io/sql.py

Lines changed: 15 additions & 6 deletions
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:

pandas/io/tests/test_sql.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,9 +1159,6 @@ def test_basic(self):
11591159

11601160
def test_write_row_by_row(self):
11611161

1162-
#FIXME: for now skip test
1163-
raise nose.SkipTest('execute not supporting cursor object')
1164-
11651162
frame = tm.makeTimeDataFrame()
11661163
frame.ix[0, 0] = np.nan
11671164
create_sql = sql.get_schema(frame, 'test', 'sqlite')
@@ -1436,9 +1433,6 @@ def test_basic(self):
14361433

14371434
def test_write_row_by_row(self):
14381435

1439-
#FIXME: for now skip test
1440-
raise nose.SkipTest('execute not supporting cursor object')
1441-
14421436
_skip_if_no_pymysql()
14431437
frame = tm.makeTimeDataFrame()
14441438
frame.ix[0, 0] = np.nan

0 commit comments

Comments
 (0)