diff --git a/pandas/io/sql.py b/pandas/io/sql.py index 6ead268b32cd6..184c1a0104703 100644 --- a/pandas/io/sql.py +++ b/pandas/io/sql.py @@ -654,8 +654,15 @@ def insert(self, chunksize=None): keys, data_list = self.insert_data() nrows = len(self.frame) + + if nrows == 0: + return + if chunksize is None: chunksize = nrows + elif chunksize == 0: + raise ValueError('chunksize argument should be non-zero') + chunks = int(nrows / chunksize) + 1 with self.pd_sql.run_transaction() as conn: diff --git a/pandas/io/tests/test_sql.py b/pandas/io/tests/test_sql.py index f02c701d97bcf..217114a00e980 100644 --- a/pandas/io/tests/test_sql.py +++ b/pandas/io/tests/test_sql.py @@ -253,6 +253,10 @@ def _to_sql(self): # Nuke table self.drop_table('test_frame1') + def _to_sql_empty(self): + self.drop_table('test_frame1') + self.pandasSQL.to_sql(self.test_frame1.iloc[:0], 'test_frame1') + def _to_sql_fail(self): self.drop_table('test_frame1') @@ -850,6 +854,9 @@ def test_read_sql_named_parameter(self): def test_to_sql(self): self._to_sql() + def test_to_sql_empty(self): + self._to_sql_empty() + def test_to_sql_fail(self): self._to_sql_fail() @@ -1346,6 +1353,9 @@ def test_read_sql_named_parameter(self): def test_to_sql(self): self._to_sql() + def test_to_sql_empty(self): + self._to_sql_empty() + def test_to_sql_fail(self): self._to_sql_fail()