Why does fetch command for sql query fail but runsqlscript work?

조회 수: 3 (최근 30일)
Bryan Yu
Bryan Yu 2016년 1월 14일
댓글: Tushar Athawale 2016년 3월 9일
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

채택된 답변

Bryan Yu
Bryan Yu 2016년 1월 19일
The answer is that there is a bug in MATLAB 2015b. In order to get this to work use set(conn, 'AutoCommit', 'off').

추가 답변 (1개)

Tushar Athawale
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?
  댓글 수: 1
Bryan Yu
Bryan Yu 2016년 1월 18일
1.
>> conn.Message
ans =
[]
2. As of now I'm just trying to run a simple query to verify connection.
'select current_date'
3. These commands are executed successfully but the curs object after running:
>> curs = fetch(curs)
cursor with properties:
Attributes: []
Data: 0
DatabaseObject: [1x1 database]
RowLimit: 0
SQLQuery: 'select current_date'
Message: 'Invalid fetch cursor.'
Type: 'Database Cursor Object'
ResultSet: 0
Cursor: 0
Statement: [1x1 com.amazon.redshift.core.jdbc41.PGJDBC41Statement]
Fetch: [1x1 com.mathworks.toolbox.database.fetchTheData]

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Database Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by