Why do i receive the error 'Too many output arguments' when using sqlite/exec command

6 views (last 30 days)
I'am trying to import and export data to a sqlite-database. Data export is possible via fetch-command and sqlquery. I expected that's possible to do the same work via exec-command (getting an cursor object and then do the fetch-command).
But always when using exec-command while assign the output to a variable, I'm getting the 'Too many output arguments' error.
(I can't use fetch because later I want execute the SQL command 'PRAGMA table_info(table_name)' )
The attached example shows the working fetch-only solution and the not working exec-solution.
  • Matlab 2017a
  • database/sqlite toolbox
%%open/ create sqlite databse
try
if exist(filename)==0
db_connect=sqlite(filename,'create');
else
db_connect=sqlite(filename,'connect');
end
catch exc
display([datestr(datetime('now')) ': ' exc.identifier]);
success = false;
close (db_connect);
return;
end
% data definition
tablename='TestTable';
col_names ={'a','b'}; % column header
data = {1 , 2}; % data
data_table = cell2table(data,'VariableNames',col_names);
%%create sql table
try
exec(db_connect,['create table ' tablename ...
'(' col_names{1} ' NUMERIC, ' col_names{2} ' NUMERIC)']);
catch exc
disp('...table already exists');
end
% insert data in sql table
insert(db_connect,tablename,col_names,data_table);
% extract dat from table via fetch
try
x=fetch(db_connect,['SELECT * FROM ' tablename]);
disp('fetch success');
catch exc
disp('fetch failed');
error([exc.identifier ':' exc.message]);
close (db_connect);
end
% extract dat from table via exec - fetch
try
curs = exec(db_connect,['SELECT * FROM ' tablename]);
a=fetch(curs);
disp('exec success');
catch exc
disp('exec failed');
disp([exc.identifier ':' exc.message]);
close (db_connect)
end
%%close database
close (db_connect)
  2 Comments
Geoff Hayes
Geoff Hayes on 1 Aug 2017
Markus - please show the full error message including the line of code that is generating the error. I know that you have said that it is the exec line, but if that is
curs = exec(db_connect,['SELECT * FROM ' tablename]);
then this seems reasonable given execute SQL statement.
Markus Meißner
Markus Meißner on 2 Aug 2017
The Error occurs in line 39.
Error using sqlite/exec
Too many output arguments.
Error in example (line 39)
curs = exec(db_connect,['SELECT * FROM ' tablename]);

Sign in to comment.

Accepted Answer

Daniel Burke
Daniel Burke on 4 Aug 2017
Hi Markus, I ran your code on my own SQLite database and got the same error as you did and noticed something curious that is not entirely clear on the SQLite or exec documentation pages. In the Exec documentation page
https://www-integ3.mathworks.com/help/releases/R2018a/database/ug/exec.html?searchHighlight=exec&s_tid=doc_srchtitle
it says the following
“exec(conn,sqlquery) performs database operations on a SQLite database file by executing the SQL statement sqlquery for the SQLite connection conn using the MATLAB® interface to SQLite.”
However it does not mention SQLite in the examples below where it sets the result of exec to the variable curs. I set up another connection to a database and it looks like the SQLite connection is a different type of object than a normal database connection object in MATLAB. It doesn’t look like it is possible to set the value of exec to be a cursor object for a SQLite connection made using the sqlite function, however you should be able to get a proper connection object if you instead establish the connection with the JDBC driver and database function.
https://www.mathworks.com/help/database/ug/sqlite-jdbc-windows.html#bt8kopk-3
You can interface with it from the command line as they show at the bottom of the doc page and this object should let you assign the result of exec to be a cursor object.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!