diff --git a/pandas/io/sql.py b/pandas/io/sql.py index 4c0c22da63848..8da6a9cc4d9f5 100644 --- a/pandas/io/sql.py +++ b/pandas/io/sql.py @@ -614,7 +614,7 @@ def __init__(self, engine, meta=None): meta = MetaData(self.engine) meta.reflect(self.engine) - self.meta = meta + self._meta = meta def execute(self, *args, **kwargs): """Simple passthrough to SQLAlchemy engine""" @@ -651,6 +651,12 @@ def to_sql(self, frame, name, if_exists='fail', index=True): name, self, frame=frame, index=index, if_exists=if_exists) table.insert() + @property + def meta(self): + self._meta.clear() + self._meta.reflect() + return self._meta + @property def tables(self): return self.meta.tables diff --git a/pandas/io/tests/test_sql.py b/pandas/io/tests/test_sql.py index 9ecb605def400..7baa1bd8ea0ba 100644 --- a/pandas/io/tests/test_sql.py +++ b/pandas/io/tests/test_sql.py @@ -373,6 +373,19 @@ def test_tquery(self): row = iris_results[0] tm.equalContents(row, [5.1, 3.5, 1.4, 0.2, 'Iris-setosa']) + def test_has_table(self): + sql.to_sql(self.test_frame1, 'test_frame_has_table', self.conn, flavor='sqlite') + + self.assertTrue( + sql.has_table('test_frame_has_table', self.conn, flavor='sqlite'), + "Table not found with has_table but exists") + + self.drop_table('test_frame_has_table') + + self.assertFalse( + sql.has_table('test_frame_has_table', self.conn, flavor='sqlite'), + "Table found with has_table but does not exist") + def test_date_parsing(self): """ Test date parsing in read_sql """ # No Parsing @@ -475,6 +488,22 @@ def test_drop_table(self): self.assertFalse( temp_conn.has_table('temp_frame'), 'Table not deleted from DB') + def test_has_table(self): + temp_frame = DataFrame( + {'one': [1., 2., 3., 4.], 'two': [4., 3., 2., 1.]}) + + self.pandasSQL.to_sql(temp_frame, 'test_frame_has_table') + + self.assertTrue( + self.pandasSQL.has_table('test_frame_has_table'), + "Table not found with has_table but exists") + + self.drop_table('test_frame_has_table') + + self.assertFalse( + self.pandasSQL.has_table('test_frame_has_table'), + "Table found with has_table but does not exist") + def test_roundtrip(self): self._roundtrip()