@@ -289,7 +289,7 @@ def read_sql_table(table_name, con, schema=None, index_col=None,
289
289
----------
290
290
table_name : string
291
291
Name of SQL table in database
292
- con : SQLAlchemy connectable
292
+ con : SQLAlchemy connectable (or database string URI)
293
293
Sqlite DBAPI connection mode not supported
294
294
schema : string, default None
295
295
Name of SQL schema in database to query (if database flavor
@@ -328,6 +328,8 @@ def read_sql_table(table_name, con, schema=None, index_col=None,
328
328
read_sql
329
329
330
330
"""
331
+
332
+ con = _engine_builder (con )
331
333
if not _is_sqlalchemy_connectable (con ):
332
334
raise NotImplementedError ("read_sql_table only supported for "
333
335
"SQLAlchemy connectable." )
@@ -362,7 +364,8 @@ def read_sql_query(sql, con, index_col=None, coerce_float=True, params=None,
362
364
----------
363
365
sql : string
364
366
SQL query to be executed
365
- con : SQLAlchemy connectable(engine/connection) or sqlite3 DBAPI2 connection
367
+ con : SQLAlchemy connectable(engine/connection) or database string URI
368
+ or sqlite3 DBAPI2 connection
366
369
Using SQLAlchemy makes it possible to use any DB supported by that
367
370
library.
368
371
If a DBAPI2 object, only sqlite3 is supported.
@@ -420,7 +423,8 @@ def read_sql(sql, con, index_col=None, coerce_float=True, params=None,
420
423
----------
421
424
sql : string
422
425
SQL query to be executed or database table name.
423
- con : SQLAlchemy connectable(engine/connection) or DBAPI2 connection (fallback mode)
426
+ con : SQLAlchemy connectable(engine/connection) or database string URI
427
+ or DBAPI2 connection (fallback mode)
424
428
Using SQLAlchemy makes it possible to use any DB supported by that
425
429
library.
426
430
If a DBAPI2 object, only sqlite3 is supported.
@@ -504,7 +508,8 @@ def to_sql(frame, name, con, flavor='sqlite', schema=None, if_exists='fail',
504
508
frame : DataFrame
505
509
name : string
506
510
Name of SQL table
507
- con : SQLAlchemy connectable(engine/connection) or sqlite3 DBAPI2 connection
511
+ con : SQLAlchemy connectable(engine/connection) or database string URI
512
+ or sqlite3 DBAPI2 connection
508
513
Using SQLAlchemy makes it possible to use any DB supported by that
509
514
library.
510
515
If a DBAPI2 object, only sqlite3 is supported.
@@ -584,6 +589,22 @@ def has_table(table_name, con, flavor='sqlite', schema=None):
584
589
"MySQL will be further supported with SQLAlchemy connectables." )
585
590
586
591
592
+ def _engine_builder (con ):
593
+ """
594
+ Returns a SQLAlchemy engine from a URI (if con is a string)
595
+ else it just return con without modifying it
596
+ """
597
+ if isinstance (con , string_types ):
598
+ try :
599
+ import sqlalchemy
600
+ con = sqlalchemy .create_engine (con )
601
+ return con
602
+
603
+ except ImportError :
604
+ _SQLALCHEMY_INSTALLED = False
605
+
606
+ return con
607
+
587
608
def pandasSQL_builder (con , flavor = None , schema = None , meta = None ,
588
609
is_cursor = False ):
589
610
"""
@@ -592,6 +613,7 @@ def pandasSQL_builder(con, flavor=None, schema=None, meta=None,
592
613
"""
593
614
# When support for DBAPI connections is removed,
594
615
# is_cursor should not be necessary.
616
+ con = _engine_builder (con )
595
617
if _is_sqlalchemy_connectable (con ):
596
618
return SQLDatabase (con , schema = schema , meta = meta )
597
619
else :
0 commit comments