Importing a Database to MATLAB error

2019-05-21 01:16发布

问题:

I am trying to import tables to my MATLAB workspace and it keeps throwing me an error, "Undefined function or method 'fetch' for input arguments of type 'struct'."

This is my code that i am trying to execute:

dyn_conformer = exec(conn, 'SELECT * FROM dyn_conformer'); 
rs =fetch(dyn_conformer);

When i opened the object in the workspace, it stated that it was "Invalid or closed connection". However, previously i manage to import other tables and it seemed to execute perfectly (they are stored as a cursor object). All of a sudden, its throwing an error for other tables that i am trying to import.

I am connected to the datatabase, conn = database('postgres','username','password','org.postgresql.Driver','jdbc:postgresql://localhost:5433/postgres');.

回答1:

I would recommend you to check for existing of connection variable and that it is the properly established connection before running exec. You can do it like this:

if ~exist('conn','var') || ~isconnection(conn)
    conn = database('postgres','username','password','org.postgresql.Driver',...
           'jdbc:postgresql://localhost:5433/postgres');
else
    dyn_conformer = exec(conn, 'SELECT * FROM dyn_conformer'); 
    rs =fetch(dyn_conformer);
end