Database Toolbox cannot store vectors or strings
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I'm trying to store and retrieve data in an SQLite database with Matlab's Database toolbox. When I try to store strings or vectors as directed by the documentation and then retrieve them I get NaN instead. Additionally, the data should come back as a table but instead, it comes back as a numeric matrix (which may explain the NaN?).
Here is the code:
% create a database from scratch (this is the only documented method I could find)
dbfile=fullfile('test_database.db');
conn=sqlite(dbfile,'create');
close(conn); % this type of interface can be useful but cannot handle null data
% and is not very feature rich
% now we connect to and populate the actual database
% (requires installing a driver first)
conn=database('SQLite','','','org.sqlite.JDBC',['jdbc:sqlite:',dbfile]);
% allow foreign keys so one table can link to other tables
curs=exec(conn,'PRAGMA foreign_keys=ON');
% create a table to store things
create_test_table=['create table test_table (testID NUMERIC PRIMARY KEY,',...
' test_string VARCHAR, test_vector NUMERIC, test_scalar NUMERIC)'];
curs=exec(conn,create_test_table);
% create a new record
datainsert(conn,'test_table','testID',1)
% add data to that record
where_clause=['WHERE testID = ',num2str(1)];
update(conn,'test_table','test_string',{'string1'},where_clause)
update(conn,'test_table','test_vector',{[1,2,3,4]},where_clause)
update(conn,'test_table','test_scalar',1,where_clause)
% now view the data
sqlquery='SELECT * FROM test_table';
data_to_view=fetch(conn,sqlquery)
% same thing happens with this command
% % curs=exec(conn,sqlquery)
% % curs=fetch(curs)
% % curs.Data
ACTUAL RESULT:
data_to_view =
1 NaN NaN 1
data_to_view =
1×4 table
testID test_string test_vector test_scalar
_____________ ___________ ______________ ________
1 'string1' 1x4 double 1
The following also returns NaN:
sqlquery='SELECT test_string FROM test_table';
data_to_view=fetch(conn,sqlquery)
0 commentaires
Réponses (0)
Voir également
Catégories
En savoir plus sur Database Toolbox dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!