MATLAB: Do I get a “??? Function ‘closeSqlStatement’ is not defined for a first argument of class ‘double’.” error when attemping to close the cursor object using the CLOSE command from the Database Toolbox 3.3 (R2007a)

Database Toolbox

After running a query and using the FETCH command as follows:
curs = exec(conn,['select * from ' tableName1]);
curs = fetch(curs,Ninitial);
Data = curs.data;
close(curs);
I get the following error when I try to close my cursor object using the CLOSE command:
??? Function 'closeSqlStatement' is not defined for a first argument of class 'double'.
Error in ==> cursor.close at 20
message = closeSqlStatement(cursor.Cursor,cursor.Statement);
Error in ==> test_tdb_connect at 44
close(curs);

Best Answer

The error message indicates that the cursor object is invalid or is no longer valid, possibly due to a connection problem.
The errors can be displayed as they happen by setting the following preferences:
setdbprefs('ErrorHandling','report')
This way, the DATABASE, EXEC and/or FETCH commands will return errors when they occur instead of just storing them in the Message field. More information is available by executing the following command at the MATLAB command prompt:
doc setdbprefs
You can also check the validity of Cursor objects, by inspecting their Message and ResultSet fields.
The Visual Query Builder uses this field to check the validity of Cursor objects. You can use code similar to this to do the same:
% Execute the statement
curs = exec(conn,querystr);
% Exit if error returned from query
if ~isempty(curs.Message)
errordlg(curs.Message)
close(conn)
end
% No data returned by query or bad query
ansr = struct(curs);
if isempty(ansr.ResultSet)
errordlg('Invalid SQL statement')
close(conn)
end