Parsing the Ms access data base

2 ビュー (過去 30 日間)
Gopalakrishnan venkatesan
Gopalakrishnan venkatesan 2017 年 3 月 28 日
コメント済み: Guillaume 2017 年 3 月 29 日
I have a database with two tables. I tried to extract the data from the tables and i was able to extract also using the command below,
rs=invoke(hopen,'OpenRecordset','Data');
fieldlist=get(rs,'Fields');
ncols=get(fieldlist,'Count');
for c=1:double(ncols)
fields{c} = get(fieldlist,'Item',c-1);
resultset.columnnames{c} = get(fields{c},'Name');
end;
nrecs=0;
while get(rs,'EOF') == 0
nrecs = nrecs + 1;
for c=1:ncols
resultset.data{nrecs,c} = get(fields{c},'Value');
end;
invoke(rs,'MoveNext');
end
Only problem is when I have a multiple values in the cell i can able to extract only first element in the cell.
For example,
in the above figure, the column carsSelected consists of multiple value in the cell. In this case i cannot able to extract all the elements in the cell? How can I do this?
Thanks a lot
  4 件のコメント
Guillaume
Guillaume 2017 年 3 月 28 日
Can you show the design view of that Color table?
Gopalakrishnan venkatesan
Gopalakrishnan venkatesan 2017 年 3 月 28 日

サインインしてコメントする。

採用された回答

Guillaume
Guillaume 2017 年 3 月 28 日
Well, I learnt something today. Access does allow multivalued fields. However, it does so by hiding much of the machinery that you'd use in a normal database design (that is it hides the table modeling the relationship together with the queries required to retrieve the records).
It looks like you're using DAO to connect to the database. When I first tried, using the older ADO technology, the value property of the field is the display string (i.e. '1; 2; 3; 4'). However, with the newer DAO, according to MSDN the value property of a multivalued field of a recordset is itself a recordset. So to retrieve all the values, you'll need another loop iterating over this child recordset.
So I assume this will work:
for c=1:ncols
if fields{c}.IsComplex %true for multivalued
rs = fields{c}.Value;
value = [];
rs.MoveFirst;
while ~rs.EOF
subfield = get(rs.Fields, 'Item', 0);
value = [value, subfield.Value];
end
resulset.data{nrecs, c} = value;
else
resultset.data{nrecs,c} = fields{c}.Value;
end
end
Note that you can replace most of the get(...) access by direct dotted access as I've done above. The exception appears to be the Item property that matlab refuses to see other than with get.
  2 件のコメント
Gopalakrishnan venkatesan
Gopalakrishnan venkatesan 2017 年 3 月 29 日
Thats great. Works really good. Thanks a lot
one step was missing. I have added it now,
for c=1:ncols
if fields{c}.IsComplex %true for multivalued
rs = fields{c}.Value;
value = [];
rs.MoveFirst;
while ~rs.EOF
subfield = get(rs.Fields, 'Item', 0);
value = [value, subfield.Value];
rs.MoveNext
end
resulset.data{nrecs, c} = value;
else
resultset.data{nrecs,c} = fields{c}.Value;
end
end
Guillaume
Guillaume 2017 年 3 月 29 日
Do'h! Yes of course, you need to move through the records.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDatabase Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by