@@ -663,6 +663,11 @@ def psql_insert_copy(table, conn, keys, data_iter):
663
663
tm .assert_frame_equal (result , expected )
664
664
665
665
666
+ def test_execute_typeerror (sqlite_iris_engine ):
667
+ with pytest .raises (TypeError , match = "pandas.io.sql.execute requires a connection" ):
668
+ sql .execute ("select * from iris" , sqlite_iris_engine )
669
+
670
+
666
671
class MixInBase :
667
672
def teardown_method (self ):
668
673
# if setup fails, there may not be a connection to close.
@@ -956,7 +961,8 @@ def test_roundtrip_chunksize(self, test_frame1):
956
961
957
962
def test_execute_sql (self ):
958
963
# drop_sql = "DROP TABLE IF EXISTS test" # should already be done
959
- iris_results = sql .execute ("SELECT * FROM iris" , con = self .conn )
964
+ with sql .pandasSQL_builder (self .conn ) as pandas_sql :
965
+ iris_results = pandas_sql .execute ("SELECT * FROM iris" )
960
966
row = iris_results .fetchone ()
961
967
tm .equalContents (row , [5.1 , 3.5 , 1.4 , 0.2 , "Iris-setosa" ])
962
968
@@ -2795,7 +2801,8 @@ def format_query(sql, *args):
2795
2801
2796
2802
def tquery (query , con = None ):
2797
2803
"""Replace removed sql.tquery function"""
2798
- res = sql .execute (query , con = con ).fetchall ()
2804
+ with sql .pandasSQL_builder (con ) as pandas_sql :
2805
+ res = pandas_sql .execute (query ).fetchall ()
2799
2806
return None if res is None else list (res )
2800
2807
2801
2808
@@ -2860,7 +2867,8 @@ def test_execute(self, sqlite_buildin):
2860
2867
ins = "INSERT INTO test VALUES (?, ?, ?, ?)"
2861
2868
2862
2869
row = frame .iloc [0 ]
2863
- sql .execute (ins , sqlite_buildin , params = tuple (row ))
2870
+ with sql .pandasSQL_builder (sqlite_buildin ) as pandas_sql :
2871
+ pandas_sql .execute (ins , tuple (row ))
2864
2872
sqlite_buildin .commit ()
2865
2873
2866
2874
result = sql .read_sql ("select * from test" , sqlite_buildin )
@@ -2895,11 +2903,12 @@ def test_execute_fail(self, sqlite_buildin):
2895
2903
cur = sqlite_buildin .cursor ()
2896
2904
cur .execute (create_sql )
2897
2905
2898
- sql .execute ('INSERT INTO test VALUES("foo", "bar", 1.234)' , sqlite_buildin )
2899
- sql .execute ('INSERT INTO test VALUES("foo", "baz", 2.567)' , sqlite_buildin )
2906
+ with sql .pandasSQL_builder (sqlite_buildin ) as pandas_sql :
2907
+ pandas_sql .execute ('INSERT INTO test VALUES("foo", "bar", 1.234)' )
2908
+ pandas_sql .execute ('INSERT INTO test VALUES("foo", "baz", 2.567)' )
2900
2909
2901
- with pytest .raises (sql .DatabaseError , match = "Execution failed on sql" ):
2902
- sql .execute ('INSERT INTO test VALUES("foo", "bar", 7)' , sqlite_buildin )
2910
+ with pytest .raises (sql .DatabaseError , match = "Execution failed on sql" ):
2911
+ pandas_sql .execute ('INSERT INTO test VALUES("foo", "bar", 7)' )
2903
2912
2904
2913
def test_execute_closed_connection (self ):
2905
2914
create_sql = """
@@ -2915,7 +2924,8 @@ def test_execute_closed_connection(self):
2915
2924
cur = conn .cursor ()
2916
2925
cur .execute (create_sql )
2917
2926
2918
- sql .execute ('INSERT INTO test VALUES("foo", "bar", 1.234)' , conn )
2927
+ with sql .pandasSQL_builder (conn ) as pandas_sql :
2928
+ pandas_sql .execute ('INSERT INTO test VALUES("foo", "bar", 1.234)' )
2919
2929
2920
2930
msg = "Cannot operate on a closed database."
2921
2931
with pytest .raises (sqlite3 .ProgrammingError , match = msg ):
0 commit comments