@@ -76,6 +76,17 @@ def _parse_date_columns(data_frame, parse_dates):
76
76
return data_frame
77
77
78
78
79
+ def _is_sqlalchemy_engine (con ):
80
+ try :
81
+ import sqlalchemy
82
+ if isinstance (con , sqlalchemy .engine .Engine ):
83
+ return True
84
+ else :
85
+ return False
86
+ except ImportError :
87
+ return False
88
+
89
+
79
90
def execute (sql , con , cur = None , params = None ):
80
91
"""
81
92
Execute the given SQL query using the provided connection object.
@@ -262,7 +273,15 @@ def read_sql_table(table_name, con, index_col=None, coerce_float=True,
262
273
263
274
264
275
"""
265
- pandas_sql = PandasSQLAlchemy (con )
276
+ import sqlalchemy
277
+ from sqlalchemy .schema import MetaData
278
+ meta = MetaData (con )
279
+ try :
280
+ meta .reflect (only = [table_name ])
281
+ except sqlalchemy .exc .InvalidRequestError :
282
+ raise ValueError ("Table %s not found" % table_name )
283
+
284
+ pandas_sql = PandasSQLAlchemy (con , meta = meta )
266
285
table = pandas_sql .read_table (
267
286
table_name , index_col = index_col , coerce_float = coerce_float ,
268
287
parse_dates = parse_dates , columns = columns )
@@ -380,6 +399,7 @@ def read_sql(sql, con, index_col=None, coerce_float=True, params=None,
380
399
coerce_float = coerce_float , parse_dates = parse_dates )
381
400
382
401
if pandas_sql .has_table (sql ):
402
+ pandas_sql .meta .reflect (only = [sql ])
383
403
return pandas_sql .read_table (
384
404
sql , index_col = index_col , coerce_float = coerce_float ,
385
405
parse_dates = parse_dates , columns = columns )
@@ -471,17 +491,9 @@ def pandasSQL_builder(con, flavor=None, meta=None, is_cursor=False):
471
491
"""
472
492
# When support for DBAPI connections is removed,
473
493
# is_cursor should not be necessary.
474
- try :
475
- import sqlalchemy
476
-
477
- if isinstance (con , sqlalchemy .engine .Engine ):
478
- return PandasSQLAlchemy (con , meta = meta )
479
- else :
480
- if flavor == 'mysql' :
481
- warnings .warn (_MYSQL_WARNING , FutureWarning )
482
- return PandasSQLLegacy (con , flavor , is_cursor = is_cursor )
483
-
484
- except ImportError :
494
+ if _is_sqlalchemy_engine (con ):
495
+ return PandasSQLAlchemy (con , meta = meta )
496
+ else :
485
497
if flavor == 'mysql' :
486
498
warnings .warn (_MYSQL_WARNING , FutureWarning )
487
499
return PandasSQLLegacy (con , flavor , is_cursor = is_cursor )
@@ -767,7 +779,6 @@ def __init__(self, engine, meta=None):
767
779
if not meta :
768
780
from sqlalchemy .schema import MetaData
769
781
meta = MetaData (self .engine )
770
- meta .reflect (self .engine )
771
782
772
783
self .meta = meta
773
784
@@ -812,19 +823,16 @@ def tables(self):
812
823
return self .meta .tables
813
824
814
825
def has_table (self , name ):
815
- if self .meta .tables .get (name ) is not None :
816
- return True
817
- else :
818
- return False
826
+ return self .engine .has_table (name )
819
827
820
828
def get_table (self , table_name ):
821
829
return self .meta .tables .get (table_name )
822
830
823
831
def drop_table (self , table_name ):
824
832
if self .engine .has_table (table_name ):
833
+ self .meta .reflect (only = [table_name ])
825
834
self .get_table (table_name ).drop ()
826
835
self .meta .clear ()
827
- self .meta .reflect ()
828
836
829
837
def _create_sql_schema (self , frame , table_name ):
830
838
table = PandasSQLTable (table_name , self , frame = frame )
0 commit comments