Skip to content

ENH: ValueError when to_sql() receives empty name param #52675

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 4 commits into from
Apr 17, 2023
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ MultiIndex
I/O
^^^
- :meth:`DataFrame.to_orc` now raising ``ValueError`` when non-default :class:`Index` is given (:issue:`51828`)
- :meth:`DataFrame.to_sql` now raising ``ValueError`` when the name param is left empty while using SQLAlchemy to connect (:issue:`52675`)
- Bug in :func:`read_html`, style elements were read into DataFrames (:issue:`52197`)
- Bug in :func:`read_html`, tail texts were removed together with elements containing ``display:none`` style (:issue:`51629`)
-
Expand Down
3 changes: 3 additions & 0 deletions pandas/io/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,9 @@ def __init__(
if self.table is None:
raise ValueError(f"Could not init table '{name}'")

if not len(self.name):
raise ValueError("Empty table name specified")

def exists(self):
return self.pd_sql.has_table(self.name, self.schema)

Expand Down
5 changes: 5 additions & 0 deletions pandas/tests/io/test_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -2676,6 +2676,11 @@ def test_bigint_warning(self):
with tm.assert_produces_warning(None):
sql.read_sql_table("test_bigintwarning", self.conn)

def test_valueerror_exception(self):
df = DataFrame({"col1": [1, 2], "col2": [3, 4]})
with pytest.raises(ValueError, match="Empty table name specified"):
df.to_sql(name="", con=self.conn, if_exists="replace", index=False)

def test_row_object_is_named_tuple(self):
# GH 40682
# Test for the is_named_tuple() function
Expand Down