@@ -367,6 +367,14 @@ def _have_unread_result(self):
367
367
except AttributeError :
368
368
return False
369
369
370
+ def _check_executed (self ):
371
+ """Check if the statement has been executed.
372
+
373
+ Raises an error if the statement has not been executed.
374
+ """
375
+ if self ._executed is None :
376
+ raise errors .InterfaceError (ERR_NO_RESULT_TO_FETCH )
377
+
370
378
def next (self ):
371
379
"""Used for iterating over the result set."""
372
380
return self .__next__ ()
@@ -667,6 +675,7 @@ def executemany(self, operation, seq_params):
667
675
return None
668
676
stmt = self ._batch_insert (operation , seq_params )
669
677
if stmt is not None :
678
+ self ._executed = stmt
670
679
return self .execute (stmt )
671
680
672
681
rowcnt = 0
@@ -875,12 +884,11 @@ def fetchone(self):
875
884
876
885
Returns a tuple or None.
877
886
"""
878
- row = self ._fetch_row ()
879
- if row :
880
- return row
881
- return None
887
+ self ._check_executed ()
888
+ return self ._fetch_row ()
882
889
883
890
def fetchmany (self , size = None ):
891
+ self ._check_executed ()
884
892
res = []
885
893
cnt = (size or self .arraysize )
886
894
while cnt > 0 and self ._have_unread_result ():
@@ -891,8 +899,10 @@ def fetchmany(self, size=None):
891
899
return res
892
900
893
901
def fetchall (self ):
902
+ self ._check_executed ()
894
903
if not self ._have_unread_result ():
895
- raise errors .InterfaceError ("No result set to fetch from." )
904
+ return []
905
+
896
906
(rows , eof ) = self ._connection .get_rows ()
897
907
if self ._nextrow [0 ]:
898
908
rows .insert (0 , self ._nextrow [0 ])
@@ -995,20 +1005,19 @@ def fetchone(self):
995
1005
996
1006
Returns a tuple or None.
997
1007
"""
998
- row = self ._fetch_row ()
999
- if row :
1000
- return row
1001
- return None
1008
+ self ._check_executed ()
1009
+ return self ._fetch_row ()
1002
1010
1003
1011
def fetchall (self ):
1004
- if self ._rows is None :
1005
- raise errors .InterfaceError ("No result set to fetch from." )
1012
+ if self ._executed is None or self . _rows is None :
1013
+ raise errors .InterfaceError (ERR_NO_RESULT_TO_FETCH )
1006
1014
res = []
1007
1015
res = self ._rows [self ._next_row :]
1008
1016
self ._next_row = len (self ._rows )
1009
1017
return res
1010
1018
1011
1019
def fetchmany (self , size = None ):
1020
+ self ._check_executed ()
1012
1021
res = []
1013
1022
cnt = (size or self .arraysize )
1014
1023
while cnt > 0 :
@@ -1032,15 +1041,13 @@ class MySQLCursorRaw(MySQLCursor):
1032
1041
_raw = True
1033
1042
1034
1043
def fetchone (self ):
1035
- row = self ._fetch_row (raw = True )
1036
-
1037
- if row :
1038
- return row
1039
- return None
1044
+ self ._check_executed ()
1045
+ return self ._fetch_row (raw = True )
1040
1046
1041
1047
def fetchall (self ):
1048
+ self ._check_executed ()
1042
1049
if not self ._have_unread_result ():
1043
- raise errors . InterfaceError ( "No result set to fetch from." )
1050
+ return []
1044
1051
(rows , eof ) = self ._connection .get_rows (raw = True )
1045
1052
if self ._nextrow [0 ]:
1046
1053
rows .insert (0 , self ._nextrow [0 ])
@@ -1071,14 +1078,11 @@ def _handle_resultset(self):
1071
1078
pass
1072
1079
1073
1080
def fetchone (self ):
1074
- row = self ._fetch_row ()
1075
- if row :
1076
- return row
1077
- return None
1081
+ self ._check_executed ()
1082
+ return self ._fetch_row ()
1078
1083
1079
1084
def fetchall (self ):
1080
- if self ._rows is None :
1081
- raise errors .InterfaceError ("No result set to fetch from." )
1085
+ self ._check_executed ()
1082
1086
return [r for r in self ._rows [self ._next_row :]]
1083
1087
1084
1088
@property
@@ -1247,11 +1251,13 @@ def fetchone(self):
1247
1251
1248
1252
Returns a tuple or None.
1249
1253
"""
1254
+ self ._check_executed ()
1250
1255
if self ._cursor_exists :
1251
1256
self ._connection .cmd_stmt_fetch (self ._prepared ['statement_id' ])
1252
1257
return self ._fetch_row () or None
1253
1258
1254
1259
def fetchmany (self , size = None ):
1260
+ self ._check_executed ()
1255
1261
res = []
1256
1262
cnt = (size or self .arraysize )
1257
1263
while cnt > 0 and self ._have_unread_result ():
@@ -1262,8 +1268,7 @@ def fetchmany(self, size=None):
1262
1268
return res
1263
1269
1264
1270
def fetchall (self ):
1265
- if not self ._have_unread_result ():
1266
- raise errors .InterfaceError ("No result set to fetch from." )
1271
+ self ._check_executed ()
1267
1272
rows = []
1268
1273
if self ._nextrow [0 ]:
1269
1274
rows .append (self ._nextrow [0 ])
@@ -1305,6 +1310,7 @@ def _row_to_python(self, rowdata, desc=None):
1305
1310
def fetchone (self ):
1306
1311
"""Returns next row of a query result set
1307
1312
"""
1313
+ self ._check_executed ()
1308
1314
row = self ._fetch_row ()
1309
1315
if row :
1310
1316
return self ._row_to_python (row , self .description )
@@ -1313,8 +1319,10 @@ def fetchone(self):
1313
1319
def fetchall (self ):
1314
1320
"""Returns all rows of a query result set
1315
1321
"""
1322
+ self ._check_executed ()
1316
1323
if not self ._have_unread_result ():
1317
- raise errors .InterfaceError (ERR_NO_RESULT_TO_FETCH )
1324
+ return []
1325
+
1318
1326
(rows , eof ) = self ._connection .get_rows ()
1319
1327
if self ._nextrow [0 ]:
1320
1328
rows .insert (0 , self ._nextrow [0 ])
@@ -1359,6 +1367,7 @@ def _row_to_python(self, rowdata, desc=None):
1359
1367
def fetchone (self ):
1360
1368
"""Returns next row of a query result set
1361
1369
"""
1370
+ self ._check_executed ()
1362
1371
row = self ._fetch_row ()
1363
1372
if row :
1364
1373
if hasattr (self ._connection , 'converter' ):
@@ -1369,8 +1378,10 @@ def fetchone(self):
1369
1378
def fetchall (self ):
1370
1379
"""Returns all rows of a query result set
1371
1380
"""
1381
+ self ._check_executed ()
1372
1382
if not self ._have_unread_result ():
1373
- raise errors .InterfaceError (ERR_NO_RESULT_TO_FETCH )
1383
+ return []
1384
+
1374
1385
(rows , eof ) = self ._connection .get_rows ()
1375
1386
if self ._nextrow [0 ]:
1376
1387
rows .insert (0 , self ._nextrow [0 ])
@@ -1392,6 +1403,7 @@ class MySQLCursorBufferedDict(MySQLCursorDict, MySQLCursorBuffered):
1392
1403
def fetchone (self ):
1393
1404
"""Returns next row of a query result set
1394
1405
"""
1406
+ self ._check_executed ()
1395
1407
row = self ._fetch_row ()
1396
1408
if row :
1397
1409
return self ._row_to_python (row , self .description )
@@ -1400,7 +1412,7 @@ def fetchone(self):
1400
1412
def fetchall (self ):
1401
1413
"""Returns all rows of a query result set
1402
1414
"""
1403
- if self ._rows is None :
1415
+ if self ._executed is None or self . _rows is None :
1404
1416
raise errors .InterfaceError (ERR_NO_RESULT_TO_FETCH )
1405
1417
res = []
1406
1418
for row in self ._rows [self ._next_row :]:
@@ -1417,6 +1429,7 @@ class MySQLCursorBufferedNamedTuple(MySQLCursorNamedTuple, MySQLCursorBuffered):
1417
1429
def fetchone (self ):
1418
1430
"""Returns next row of a query result set
1419
1431
"""
1432
+ self ._check_executed ()
1420
1433
row = self ._fetch_row ()
1421
1434
if row :
1422
1435
return self ._row_to_python (row , self .description )
@@ -1425,7 +1438,7 @@ def fetchone(self):
1425
1438
def fetchall (self ):
1426
1439
"""Returns all rows of a query result set
1427
1440
"""
1428
- if self ._rows is None :
1441
+ if self ._executed is None or self . _rows is None :
1429
1442
raise errors .InterfaceError (ERR_NO_RESULT_TO_FETCH )
1430
1443
res = []
1431
1444
for row in self ._rows [self ._next_row :]:
0 commit comments