Skip to content

Commit e7343f7

Browse files
ENH: ValueError when to_sql() receives empty name param (#52675)
* ENH: ValueError when to_sql() receives empty name param * TST: added test for issue #52675 * DOC what's new added for issue #52675 * DOC fix typo
1 parent 35a121e commit e7343f7

File tree

3 files changed

+9
-0
lines changed

3 files changed

+9
-0
lines changed

doc/source/whatsnew/v2.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ MultiIndex
339339
I/O
340340
^^^
341341
- :meth:`DataFrame.to_orc` now raising ``ValueError`` when non-default :class:`Index` is given (:issue:`51828`)
342+
- :meth:`DataFrame.to_sql` now raising ``ValueError`` when the name param is left empty while using SQLAlchemy to connect (:issue:`52675`)
342343
- Bug in :func:`read_html`, style elements were read into DataFrames (:issue:`52197`)
343344
- Bug in :func:`read_html`, tail texts were removed together with elements containing ``display:none`` style (:issue:`51629`)
344345
-

pandas/io/sql.py

+3
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,9 @@ def __init__(
886886
if self.table is None:
887887
raise ValueError(f"Could not init table '{name}'")
888888

889+
if not len(self.name):
890+
raise ValueError("Empty table name specified")
891+
889892
def exists(self):
890893
return self.pd_sql.has_table(self.name, self.schema)
891894

pandas/tests/io/test_sql.py

+5
Original file line numberDiff line numberDiff line change
@@ -2676,6 +2676,11 @@ def test_bigint_warning(self):
26762676
with tm.assert_produces_warning(None):
26772677
sql.read_sql_table("test_bigintwarning", self.conn)
26782678

2679+
def test_valueerror_exception(self):
2680+
df = DataFrame({"col1": [1, 2], "col2": [3, 4]})
2681+
with pytest.raises(ValueError, match="Empty table name specified"):
2682+
df.to_sql(name="", con=self.conn, if_exists="replace", index=False)
2683+
26792684
def test_row_object_is_named_tuple(self):
26802685
# GH 40682
26812686
# Test for the is_named_tuple() function

0 commit comments

Comments
 (0)