Undefined function 'fetch' for input arguments of type 'struct'. when reading and writing to an Access Database

3 views (last 30 days)
Hi all,
This is my first question - please accept my apologies if I format incorrectly.
I call the below code in an infinite loop which runs smoothly for the first ~200 iterations then catches the "Undefined function 'fetch' for input arguments of type 'struct'." error. It is worth noting that in between each iteration I open and close several other tables within the same ODBC connection before reconnecting to this one.
What has me confused is that it runs as it should for the first few hundred iterations. My hunch is that one connection may not close properly but Im not certain.
setdbprefs('DataReturnFormat', 'cellarray');
setdbprefs('NullNumberRead', 'NaN');
setdbprefs('NullStringRead', 'null');
conn = database('ACCESS DB', '', '');
curs = exec(conn, ['SELECT TABLE1.ID_DB'...
' FROM TABLE1 ']);
curs = fetch(curs);
close(curs);
close(conn);
Thanks in advance for any help!
  3 Comments

Sign in to comment.

Accepted Answer

the cyclist
the cyclist on 5 Feb 2013
My guess is that you are losing the connection to the database, but I have no idea why. I suggest you do the following. Type
>> dbstop if error
before you try to run that loop. Then execute your code. When the error occurs, the execution will halt, but you will be taken to the editor, at the line where the error occurred. You can see what the variables look like at the time of the error.
My version of your code ran for 500+ iterations, but when I deliberately broke the connection (by disconnecting my VPN), the code halted with the same error you got. The proximate cause of the error is that "curs", instead of being a full cursor object, is instead a structure that is just
curs.Message = 'Invalid connection.'
  1 Comment
Lachlan
Lachlan on 5 Feb 2013
Edited: Lachlan on 5 Feb 2013
Thanks cyclist for your response.
I have tried your suggestion which yields a different error "sun.jdbc.odbc.JdbcOdbcBatchUpdateException: General error". However in the process of doing this I think I have discovered the root cause. As the loop was running I opened up the database to confirm that the data was being written, in doing so I selected a cell inadvertently. The next loop then generates the error. This still has me a little confused as I had closed Access before re-executing the code only I got the error immediately. I think this confirms you correct that I was still connected to the DB form the previous execution and thus couldn't connect. It looks fine if I don't open it up mess around.

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!