Why do i receive the error 'Too many output arguments' when using sqlite/exec command
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
Geoff Hayes
2017-8-1
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]);
采纳的回答
Daniel Burke
2017-8-4
编辑:Rena Berman
2024-7-16
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.
0 个评论
更多回答(0 个)
另请参阅
类别
在 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!