SQL query in a M-file. Not working

1 次查看(过去 30 天)
I tried to execute an sql query from an M- file. But I am getting the error message "undefined function fetch ". when i executed the same query from workspace it is working.
I tried the following code in a M-file:
function [ Pocc ] = Multiprodinv( x )
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
conn = database('MultiProductInventory','','');
x1=x(1);
x2=x(2);
x3=x(3);
x4=x(4);
x5=x(5);
x6=x(6);
x7=x(7);
x8=x(8);
x9=x(9);
x10=x(10);
curs = exec(conn,['select all ID from Stocks where PI=',num2str(x1),'and F1=',num2str(x2),...
'and F2=',num2str(x3),'and F3=',num2str(x4),'and F4=',num2str(x5),'and F5=',num2str(x6),...
'and F6=',num2str(x7),'and F7=',num2str(x8),'and F8=',num2str(x9),'and F9=',num2str(x10)]);
curs=fetch(curs);
y= curs.Data;
Pocc=length(y);
x is a record of integer values with 10 fields representing the inventory at various levels (plants, warehouses, distributors, agents etc).The first field of x is the ProductID. The program has to find the number of occurrences of the record x in the database(MultiProductInventory) table Stocks.
please guide me.
  2 个评论
Guillaume
Guillaume 2016-8-3
You've got a computer language that allows you to automate things, and instead you go and make it more manual by typing every step of your query.
conn = database('MultiProductInventory','','');
query = ['select all ID from Stocks where PI=', num2str(x(1))];
for fidx = 1:9
query = [query, sprintf(' and F%d = ', fidx), num2str(x(fidx + 1))];
end
curs = exec(conn, query);
Isn't that less work?
No idea about your problem, though. Can you give the entire error message? Are you sure that curs is a cursor object when you call fetch from the command line?
Kallam Haranadha Reddy
I searched for MATLAB questions and answers for my problem ( SQL query not working in M file) ( error message : function fetch not defined). I am providing the link below to a similar problem where fetch is not working and runsqlscript command is working. I am using Micro soft's Access database.I am a novice to database. In the link given below , in the 'conn' ( connection to database), he has used database name, user name, password and JDBC address. I searched the MATLAB documentation for MS-Access database driver address. But i could not find it, instead there are details about oracle, SQL server and MYsql databases. i want to know whether there is any syntax error in my coding or i have to specify Access database driver address or the MATLAB does not support Access database But then, i am surprised, why the same sequence of commands, when executed from command line, are giving the results.
please guide me.

请先登录,再进行评论。

回答(0 个)

类别

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