Loading multiple files into the same database

조회 수: 2 (최근 30일)
Bob Choy
Bob Choy 2012년 12월 16일
Hi! I have the folowing function:
function loaddatabase(Name_File,Namedb)
id = fopen(Name_File, 'r');
create = 'create table Table1 ( Temperature integer, Humidity real, PRIMARY KEY(Temperature) );';
sqlite(create,Namedb);
while ~feof(id)
temperature = fscanf (id, '%d', 1);
humidity = fscanf (id, '%f\n',1);
insert = sprintf('insert into Table1 values ( %d, %f );', temperature, humidity);
sqlite(insert,Namedb);
end
end
This function creates a database from information contained a .txt file. Where Name_File is the name of that .txt file and Namedb is the name I want to give my database. My .txt files follow the following structure: file1.txt, file2.txt, etc...
My problem is that I don't want load the info of a single .txt file but rather all of them into the same database.
How can I do this?
Thank you in advance.

답변 (2개)

Mark Whirdy
Mark Whirdy 2012년 12월 16일
편집: Mark Whirdy 2012년 12월 16일
Hi Bob
Can you read the contents of all files into matlab in loop, vertically concatenating the cellarrays and perform a single fastinsert (of the vertcatted cellarray) at the end? If I'm missing the crux of the issue here, could you provide some more code to make the context clearer?
  댓글 수: 1
Bob Choy
Bob Choy 2012년 12월 16일
Im not sure what your first question was, can you type that into code so I can understand it better? I also updated the code so its hopefully clearer now.

댓글을 달려면 로그인하십시오.


Mark Whirdy
Mark Whirdy 2012년 12월 16일
In semi-pseudo code below
My_array = []; % container for all file contents
for i = 1:size(Name_File_Array)
fid = fopen(Name_File_Array{i,:}, 'r');
str = fread(fid, '*char')'; % whatever parsing is suitable for your data, since I can't see your data I have no idea what
My_array = [My_array;str]; % vertcat each file contents into a single large array
exec('Create table mytable(header1,header2,header3)');
fastinsert('mytable',My_array)
end
  댓글 수: 1
Bob Choy
Bob Choy 2012년 12월 16일
My data follows the following structure:
integer real
integer real
integer real
And so on. Where the first column are temperature values and the second humidity values. Does this help you understand how can I do this using my function above?
Thank you for your efforts so far.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Database Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by