Importing a Database to MATLAB error

纵饮孤独 提交于 2019-12-10 10:57:31

问题


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


来源:https://stackoverflow.com/questions/9141319/importing-a-database-to-matlab-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!