Skip to content

Commit b138532

Browse files
Add support for sqlite.
1 parent 2582834 commit b138532

File tree

2 files changed

+34
-12
lines changed

2 files changed

+34
-12
lines changed

pandas/io/sql.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import warnings
3131

3232
import numpy as np
33-
from sqlalchemy.exc import ProgrammingError
3433

3534
from pandas._config import using_string_dtype
3635

@@ -975,6 +974,11 @@ def _drop_temporary_table(self):
975974
self.pd_sql.execute(query)
976975

977976
def _exists_temporary(self):
977+
from sqlalchemy.exc import (
978+
OperationalError,
979+
ProgrammingError,
980+
)
981+
978982
if self.schema is None:
979983
query = f"SELECT * FROM {self.name} LIMIT 1"
980984
else:
@@ -986,6 +990,8 @@ def _exists_temporary(self):
986990
# Some DBMS (e.g. postgres) require a rollback after a caught exception
987991
self.pd_sql.execute("rollback")
988992
return False
993+
except OperationalError:
994+
return False
989995

990996
def exists(self):
991997
if self.is_temporary:
@@ -2816,6 +2822,7 @@ def to_sql(
28162822
chunksize: int | None = None,
28172823
dtype: DtypeArg | None = None,
28182824
method: Literal["multi"] | Callable | None = None,
2825+
prefixes: Sequence[str] | None = None,
28192826
engine: str = "auto",
28202827
**engine_kwargs,
28212828
) -> int | None:
@@ -2856,6 +2863,9 @@ def to_sql(
28562863
28572864
Details and a sample callable implementation can be found in the
28582865
section :ref:`insert method <io.sql.method>`.
2866+
prefixs : sequence, optional
2867+
A list of strings to insert after CREATE in the CREATE TABLE statement.
2868+
They will be separated by spaces.
28592869
"""
28602870
if dtype:
28612871
if not is_dict_like(dtype):
@@ -2881,6 +2891,7 @@ def to_sql(
28812891
if_exists=if_exists,
28822892
index_label=index_label,
28832893
dtype=dtype,
2894+
prefixes=prefixes,
28842895
)
28852896
table.create()
28862897
return table.insert(chunksize, method)

pandas/tests/io/test_sql.py

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@
1717

1818
import numpy as np
1919
import pytest
20-
from sqlalchemy import text
21-
from sqlalchemy.engine import Connection
20+
from sqlalchemy import (
21+
create_engine,
22+
text,
23+
)
24+
from sqlalchemy.engine import Engine
2225

2326
from pandas._config import using_string_dtype
2427

@@ -4353,7 +4356,7 @@ def test_xsqlite_if_exists(sqlite_buildin):
43534356
drop_table(table_name, sqlite_buildin)
43544357

43554358

4356-
@pytest.mark.parametrize("conn", mysql_connectable + postgresql_connectable)
4359+
@pytest.mark.parametrize("conn", sqlalchemy_connectable)
43574360
def test_exists_temporary_table(conn, test_frame1, request):
43584361
conn = request.getfixturevalue(conn)
43594362

@@ -4372,14 +4375,18 @@ def test_exists_temporary_table(conn, test_frame1, request):
43724375
assert True if table.exists() else False
43734376

43744377

4375-
@pytest.mark.parametrize("conn", mysql_connectable + postgresql_connectable)
4378+
@pytest.mark.parametrize("conn", sqlalchemy_connectable)
43764379
def test_to_sql_temporary_table_replace(conn, test_frame1, request):
43774380
conn = request.getfixturevalue(conn)
43784381

4379-
if isinstance(conn, Connection):
4380-
con = conn
4381-
else:
4382+
# some DBMS only allow temporary tables to exist within a connection, therefore
4383+
# we can only test for a connection and not all types of connectables.
4384+
if isinstance(conn, Engine):
43824385
con = conn.connect()
4386+
elif isinstance(conn, str):
4387+
con = create_engine(conn).connect()
4388+
else:
4389+
con = conn
43834390

43844391
test_frame1.to_sql(
43854392
name="test_frame1",
@@ -4402,14 +4409,18 @@ def test_to_sql_temporary_table_replace(conn, test_frame1, request):
44024409
assert_frame_equal(test_frame1, df_test)
44034410

44044411

4405-
@pytest.mark.parametrize("conn", mysql_connectable + postgresql_connectable)
4412+
@pytest.mark.parametrize("conn", sqlalchemy_connectable)
44064413
def test_to_sql_temporary_table_fail(conn, test_frame1, request):
44074414
conn = request.getfixturevalue(conn)
44084415

4409-
if isinstance(conn, Connection):
4410-
con = conn
4411-
else:
4416+
# some DBMS only allow temporary tables to exist within a connection, therefore
4417+
# we can only test for a connection and not all types of connectables.
4418+
if isinstance(conn, Engine):
44124419
con = conn.connect()
4420+
elif isinstance(conn, str):
4421+
con = create_engine(conn).connect()
4422+
else:
4423+
con = conn
44134424

44144425
test_frame1.to_sql(
44154426
name="test_frame1",

0 commit comments

Comments
 (0)