Skip to content

Commit e4cdf31

Browse files
add schema to "read_sql" function and catch exception of calling has_table with sql_string
1 parent 8e7f41f commit e4cdf31

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

pandas/io/sql.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ def read_sql_query(sql, con, index_col=None, coerce_float=True, params=None,
365365

366366

367367
def read_sql(sql, con, index_col=None, coerce_float=True, params=None,
368-
parse_dates=None, columns=None):
368+
parse_dates=None, columns=None, schema=None):
369369
"""
370370
Read SQL query or database table into a DataFrame.
371371
@@ -396,6 +396,7 @@ def read_sql(sql, con, index_col=None, coerce_float=True, params=None,
396396
columns : list
397397
List of column names to select from sql table (only used when reading
398398
a table).
399+
schema : Name of SQL schema in database.
399400
400401
Returns
401402
-------
@@ -421,11 +422,16 @@ def read_sql(sql, con, index_col=None, coerce_float=True, params=None,
421422
sql, index_col=index_col, params=params,
422423
coerce_float=coerce_float, parse_dates=parse_dates)
423424

424-
if pandas_sql.has_table(sql):
425-
pandas_sql.meta.reflect(only=[sql])
425+
try:
426+
_is_table_name = pandas_sql.has_table(sql, schema=schema)
427+
except:
428+
_is_table_name = False
429+
430+
if _is_table_name:
431+
pandas_sql.meta.reflect(only=[sql], schema=schema)
426432
return pandas_sql.read_table(
427433
sql, index_col=index_col, coerce_float=coerce_float,
428-
parse_dates=parse_dates, columns=columns)
434+
parse_dates=parse_dates, columns=columns, schema=schema)
429435
else:
430436
return pandas_sql.read_sql(
431437
sql, index_col=index_col, params=params,

0 commit comments

Comments
 (0)