Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

MATLab and Database --> Very urgent

galib.csegalib.cse Posts: 1Member
Hello,
I am stuck with this problem for last few weeks. I have the codes to connect to the database and then import data from there to MATLab workspace. then I performed some calculations and now I need to save this data to another table in the same database. again, i am using same connection (as i have the connection which worked for importing). But now it's not working. it's an "access" database. i am giving the codes which i found. please, help me about this. i need this very urgent.
thank you in advance.

regards,
Galib

////////////////////////////////////////////////////////
code: oledbcn
/////////////////////
function cn=oledbcn(cnstr,cto,rst)
% cn = oledb(cnstr,[cto],[rst])
%
% Tim Myers
% myers@metronaviation.com
% March 2005
% Updated January 2006
% Updated August 2006

try
%create activeX control
try
cn=COM.OWC11_DataSourceControl_11;
catch
cn=COM.OWC10_DataSourceControl_10;
end

%Open connection
cn.ConnectionString=cnstr;

%Specify connection timeout if provided
if nargin>1
cn.Connection.CommandTimeout=cto;
else
cn.Connection.CommandTimeout=60; %default
end

%Specify connection RecordsetType
if nargin>2
cn.RecordsetType=rst;
else
cn.RecordsetType=1; %dscSnapshot default
end
catch
disp('***** OLEDBCN TROUBLESHOOTING *****')
error('Could not create connection. See troubleshooting above.')
end

//////////////////////////////////////////////////////////
code: oledbcnstr
///////////////////
function s=oledbcnstr(type,sv,db,uid,pwd)
% s=oledbcnstr(type,[sv],[db],[uid],[pwd])
% Tim Myers
% myers@metronaviation.com
% March 2005

if strcmpi('ACCESS',type)
%Connect to Access database given by input db
s=['PROVIDER=MSDASQL;'];
s=[s 'DRIVER={Microsoft Access Driver (*.mdb)};'];
s=[s 'DBQ=' db ';'];

elseif strcmpi('SQL',type)
%Connect to SQL Database
s=['PROVIDER=MSDASQL;DRIVER={SQL Server};'];
s=[s 'SERVER=' sv ';DATABASE=' db ';'];

elseif strcmpi('ORACLE',type)
%Connect to Oracle Database
s=['PROVIDER=MSDASQL;'];
s=[s 'DRIVER={Microsoft ODBC for Oracle};'];
s=[s 'SERVER=' sv ';'];
else
s='Unknown type';
end

%add uid and pwd if provided
if nargin>3 s=[s 'UID=' uid ';']; end
if nargin>4 s=[s 'PWD=' pwd ';']; end

/////////////////////////////////////////////////////////
code: oledbquery
///////////////////////
function x=oledbquery(cn,sql,flag)
% [x]=oledbquery(cn,sql)

%open recordset and run query
r = cn.connection.invoke('execute', sql);

%if for retrieving or inserting data
if flag == 1 % i tried here not to get data again. %
%retrieve data from recordset
if r.state && r.recordcount>0
x=invoke(r,'getrows');
x=x';
else
x=[];
end
end
%release recordset
invoke(r,'release');

//////////////////////////////////////////////////////////
code: oledb_demo
/////////////////////
%demo_oledb
% Demonstrates the OLEDB connection functions
%
% Requires these files:
% oledbcnstr.m
% oledbcn.m
% oledbquery.m
% test.mdb
%
% Usage:
% Only 4 lines to connect, query, and disconnect:
%
% s=oledbcnstr(type,[sv],[db],[uid],[pwd]);
% cn = oledb(cnstr,[cto],[rst]);
% [x]=oledbquery(cn,sql);
% invoke(cn,'release')
%
% Tim Myers
% myers@metronaviation.com
% March 2005
clc
disp('Make sure test.mdb is in the current directory')
%Build connection string
%Google "dsn-less connection strings" for help constructing
%your own connection strings
s=oledbcnstr('Access',[],[cd ' est.mdb']);
%Open connection
cn=oledbcn(s);
%Sample query to execute
% for i=1:20
% for j=13:15
sql='select distinct spiraldataid,X,Y,time,radius,theta,DR,DT from spiraldata where questionid = 13 and testdataid = 1' ;
flag = 1
%Run query and return results
x=oledbquery(cn,sql,flag)
X = double(cell2mat(x(:,2)))
Y = double(cell2mat(x(:,3)))
T = cell2mat(x(:,4))
R = double(cell2mat(x(:,5)))
theta = double(cell2mat(x(:,6)))
DR = double(cell2mat(x(:,7)))
DT = double(cell2mat(x(:,8)))
dMean = mean(T)
dStdDev = std(T)
flag = 0
count = oledbquery(cn,sql,flag)
icount = cell2mat(count)
icount = icount + 1
% insert(cn, 'Features', 'ID', icount)
% insert(cn, 'Features', 'TestDataID', i)
% insert(cn, 'Features', 'QuestionID', j)
% insert(cn, 'Features', 'Mean', dMean)
%*****************************
% when i tried with this insert function it showed error % with connection
%***********************
sql = 'insert into Features values (icount, 1, 13, dMean)'
x = oledbquery(cn,sql,flag)
%**********************************
%in this case also error was shown
%*********************************
% end
% end
%Close connection
invoke(cn,'release')
////////////////////////////////////////////////////////////////
Please, help!!!
Sign In or Register to comment.