Skip to content

BUG/ERR: raise correct error when sql driver is not installed #14527

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions pandas/io/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,10 +507,11 @@ def _engine_builder(con):
if isinstance(con, string_types):
try:
import sqlalchemy
con = sqlalchemy.create_engine(con)
return con
except ImportError:
_SQLALCHEMY_INSTALLED = False
else:
con = sqlalchemy.create_engine(con)
return con

return con

Expand Down
8 changes: 7 additions & 1 deletion pandas/io/tests/test_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -944,7 +944,7 @@ def test_sqlalchemy_type_mapping(self):
self.assertTrue(isinstance(
table.table.c['time'].type, sqltypes.DateTime))

def test_to_sql_read_sql_with_database_uri(self):
def test_database_uri_string(self):

# Test read_sql and .to_sql method with a database URI (GH10654)
test_frame1 = self.test_frame1
Expand All @@ -963,6 +963,12 @@ def test_to_sql_read_sql_with_database_uri(self):
tm.assert_frame_equal(test_frame1, test_frame3)
tm.assert_frame_equal(test_frame1, test_frame4)

# using driver that will not be installed on Travis to trigger error
# in sqlalchemy.create_engine -> test passing of this error to user
db_uri = "postgresql+pg8000://user:pass@host/dbname"
with tm.assertRaisesRegexp(ImportError, "pg8000"):
sql.read_sql("select * from table", db_uri)

def _make_iris_table_metadata(self):
sa = sqlalchemy
metadata = sa.MetaData()
Expand Down