Querying an Access database

I one had an occasion when I needed to collect quite a large amount of data from a Microsoft Access database (.mdb) and process it using MATLAB and Windows. I didn't have the database toolbox so I needed to find another method.

It is possible to query and modify an Access database using ADO. The following code snip shows you how:

% Create an ADO recordset
connA = actxserver('ADODB.RecordSet');
try
    % Send a query to the database
    connA.Open('QUERY_GOES_HERE', DSN);
    xMin = connA.GetRows;
    xMin = xMin{1};
    connA.Close;
catch
    % If the query is unsuccessful, for what ever reason
    % we can inform the user here

end

If you don't like the ADO method, it is also possible to connect to an Access database using DAO:

% Browse for an Access database file.
[filename, pathname] = uigetfile('*.mdb', 'Select a database');
database = [pathname filename];
try
    % Create a connection handle
    h = actxserver('DAO.DBEngine.36');
    % Open a database connection
    db = h.OpenDatabase(database);
    % Send a query
    q = db.CreateQueryDef('','SELECT * FROM [table]')
    % Retrieve the result
    rs = q.OpenRecordset
catch
    % If the query is unsuccessful, for what ever reason
    % we can inform the user here

end


Warning: INSERT command denied to user 'revjbr_dan'@'10.0.0.14' for table 'watchdog' query: INSERT INTO watchdog (uid, type, message, severity, link, location, referer, hostname, timestamp) VALUES (0, 'php', '<em>INSERT command denied to user &amp;#039;revjbr_dan&amp;#039;@&amp;#039;10.0.0.14&amp;#039; for table &amp;#039;sessions&amp;#039;\nquery: INSERT INTO sessions (sid, uid, cache, hostname, session, timestamp) VALUES (&amp;#039;ddb0d1b775a514423cffba4a5961468e&amp;#039;, 0, 0, &amp;#039;38.107.179.239&amp;#039;, &amp;#039;&amp;#039;, 1328688549)</em> in <em>/home/www/danpearce.co.uk/includes/database.mysql.inc</em> on line <em>172</em>.', 2, '', 'http://www.danpearce.co.uk/matlab_query', '', '38.107.179.239', 1328688549) in /home/www/danpearce.co.uk/includes/database.mysql.inc on line 172