fetchall Method (Python)

.fetchall(). Fetches all (remaining) cases from the active dataset, or if there are splits, the remaining cases in the current split. If there are no remaining rows, the result is an empty tuple.

  • This method is available in read or write mode.
  • When used in write mode, calling fetchall will position the record pointer at the last case of the active dataset, or if there are splits, the last case of the current split.
  • Cases from the active dataset are returned as a list of tuples. Each tuple represents the data for one case, and the tuples are arranged in the same order as the cases in the active dataset. Each element in a tuple contains the data value for a specific variable. The order of variable values within a tuple is the order specified by the variable index values in the optional argument n to the Cursor class, or file order if n is omitted. For example, if n=[5,2,7] the first tuple element is the value of the variable with index value 5, the second is the variable with index value 2, and the third is the variable with index value 7.
  • String values are right-padded to the defined width of the string variable.
  • System-missing values are always converted to the Python data type None.
  • By default, user-missing values are converted to the Python data type None. You can use the SetUserMissingInclude method to specify that user-missing values be treated as valid.
  • Values of variables with time formats are returned as integers representing the number of seconds from midnight.
  • By default, values of variables with date or datetime formats are returned as integers representing the number of seconds from October 14, 1582. You can specify to convert values of those variables to Python datetime.datetime objects with the cvtDates argument to the spss.Cursor function. See the topic spss.Cursor Class (Python) for more information.
  • If a weight variable has been defined for the active dataset, then cases with zero, negative, or missing values for the weighting variable are skipped when fetching data with fetchone, fetchall, or fetchmany. If you need to retrieve all cases when weighting is in effect, then you can use the Dataset class.
  • The fetchone, fetchall, and fetchmany methods honor case filters specified with the FILTER or USE commands.
DATA LIST FREE /var1 (F) var2 (A2)  var3 (F).
BEGIN DATA
11 ab 13
21 cd 23
31 ef  33
END DATA.
BEGIN PROGRAM.
import spss
dataCursor=spss.Cursor()
dataFile=dataCursor.fetchall()
for i in enumerate(dataFile):
  print i
print dataCursor.fetchall()
dataCursor.close()
END PROGRAM.

Result

(0, (11.0, 'ab', 13.0))
(1, (21.0, 'cd', 23.0))
(2, (31.0, 'ef', 33.0))
()

fetchall with Variable Index

DATA LIST FREE /var1 var2 var3.
BEGIN DATA
1 2 3
1 4 5
2 5 7
END DATA.
BEGIN PROGRAM.
import spss
i=[0]
dataCursor=spss.Cursor(i)
oneVar=dataCursor.fetchall()
uniqueCount=len(set(oneVar))
print oneVar
print spss.GetVariableName(0), " has ", uniqueCount, " unique values."
dataCursor.close()
END PROGRAM.

Result

((1.0,), (1.0,), (2.0,))
var1  has  2  unique values.