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

2 vues (au cours des 30 derniers jours)
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 commentaires
Geoff Hayes
Geoff Hayes le 1 Août 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 le 2 Août 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]);

Connectez-vous pour commenter.

Réponse acceptée

Daniel Burke
Daniel Burke le 4 Août 2017
Modifié(e) : Rena Berman le 16 Juil 2024
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.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.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by