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'); endcatch 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)
Best Answer