Getting database table variable names and type
조회 수: 6 (최근 30일)
이전 댓글 표시
Hi,
For a project, we do the following to to get data from a database:
connection = database( '', '', '', 'org.sqlite.JDBC', url );
curs = exec( connection, sqlQuery );
queryOut = fetch( curs );
data = queryOut.Data;
However in some cases, it can return no data. In this case, we want to have an empty table with variable names and types being the same as the ones as in the database. For now we have implemented the following:
opts = databaseImportOptions( connection, sqlQuery );
data = table( 'Size' , [ 0, numel( opts.VariableNames ) ], ...
'VariableTypes', opts.VariableTypes, ...
'VariableNames', opts.SelectedVariableNames );
It works but I was wondering if there might be something more elegant to fetch varia types and names from the database. Since I am far from being well versed in using database, jdbc etc. in Matlab, it is likely that I could have mised something.
Best
댓글 수: 0
답변 (1개)
Vatsal
2023년 9월 26일
편집: Vatsal
2023년 9월 29일
I understand that you want a more elegant way to fetch variable types and names from the database. The implementation you have provided using “databaseImportOptions” to fetch variable names and types from the database and create an empty table is already a good approach this is another way you can do the same:
sqlquery = "SELECT * FROM table_name";
[results,metadata] = fetch(conn,sqlquery);
variableNames = metadata.Properties.RowNames;
variableTypes = metadata.VariableType;
data = table('Size', [0, numel(variableNames)], ...
'VariableTypes', variableTypes, ...
'VariableNames', variableNames);
You can also refer to the MATLAB documentation for "fetch" to obtain more information on its usage and syntax. The link is provided below: -
I hope this works!
댓글 수: 3
참고 항목
카테고리
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!