The answer is that there is a bug in MATLAB 2015b. In order to get this to work use set(conn, 'AutoCommit', 'off').
Why does fetch command for sql query fail but runsqlscript work?
3 次查看(过去 30 天)
显示 更早的评论
I am connecting to a PostgresQL compliant database and am unable to use the fetch command. It returns a "Invalid fetch cursor". However, if I run the same query using runsqlscript with a *.sql file, it works.
conn=database('dbname','user','pwd','postgresql-9.4.1207','jdbc:address');
resultsFail=fetch(conn,sqlquery); # Returns 'Invalid fetch cursor'
resultsGood=runsqlscript(conn,'test.sql');
# Returns
date
------------
2016-01-14
0 个评论
采纳的回答
更多回答(1 个)
Tushar Athawale
2016-1-18
Hi Bryan,
The error you are receiving is most likely due to invalid database connection or invalid SQL query. In order to troubleshoot the issue regarding the "fetch" command:
1) What is the value of the 'Message' property of the database connection? In your example, it can be checked using the following command:
>> conn.Message
2) What is the exact SQL query that you are trying to execute?
3) Please try fetching data from the database using alternative workflow as provided on: http://www.mathworks.com/help/database/ug/importing-data-using-the-fetch-function.html. In your example, please execute the following MATLAB commands:
>> curs = exec(conn,sqlquery)
>> curs = fetch(curs)
Can you execute the above commands successfully? If not, what is the exact error message that you are receiving?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Database Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!