Accessing MySQL Through Matlab
Accessing MySQL Through Matlab
uses to perform queries to the mySQL database. Requirements 1. The ODBC drivers for mySQL must already be installed. You can get these from http://spx.arizona.edu/database.htm 2. The database that will be accessed must be added as an ODBC data source. The procedure to do this is described at http://spx.arizona.edu/Database/Setup%20Instructions.pdf 3. Matlab R2006b or newer with database toolbox Matlab Functions In order to access the database first a connection to the database must be created. This can be done by the database() function. Below the function is explained. database(data source, username, password) data source should be the name of the data source in the ODBC for Windows given as a string. This has to be configured before running MATLAB and the procedure to do this is described in the Setup Database as a source on a workstation guide. username is the username the user has on the database he/she wishes to access. This should also be given as a string. password This must be the password given with the above username to access the database. Again it should be supplied to the database() function as a string. Once the connection is established queries to the database can be performed using the fetch() function. This function is structured as follows:
fetch(Connection Pointer,SQL Syntax)
Connection Pointer This is the connection created with the database() function SQL Syntax This is a string containing the desired SQL query These are the most common functions that can be used to execute queries on a database. Other functions are available from the database toolbox. Refer to the toolbox manual for more information. Example Code The code listed here will create a connection to the patientstudy database and will create a report of how many patients were added within a given period of time and which studies they participate in.
%This file demonstrates the use of the database toolbox in Matlab %It will create a connection to the patientstudy database and will %retrieve all the patients that were added within a given period of time %and will find which studies each is participating in and create an excel %file
clear all %Input data StartDate = 'MM/DD/YYYY'; EndDate = 'MM/DD/YYYY'; %It would be best if username & password were asked from the user %during runtime for security purposes dbUsername = 'Username'; %Username for the database dbPassword = 'Password'; %Passwor associated with the above username ExcelFileName = 'PatientReport.xls'; %The filename for the output excel file %Process the input data to construct the SQL query StartSlashLoc = find(StartDate == '/'); %Find the locations of the slash EndSlashLoc = find(EndDate == '/'); %in the start and end date to determine %the month, day, year %Create the SQL date in the SQL format YYYY-MM-DD SQLStartDate = [StartDate(StartSlashLoc(2)+1:end) '-'... StartDate(1:StartSlashLoc(1)-1) '-'... StartDate(StartSlashLoc(1)+1:StartSlashLoc(2)-1) ]; SQLEndDate = [EndDate(EndSlashLoc(2)+1:end) '-'... EndDate(1:EndSlashLoc(1)-1) '-'... EndDate(EndSlashLoc(1)+1:EndSlashLoc(2)-1) ]; %Construct the SQL query SQLquery = ['select ID from patient where create_date > ''' ... SQLStartDate ''' and create_date < ''' SQLEndDate '''']; %Create the connection to the database with the credentials specified above NewConn = database('patientstudy',dbUsername,dbPassword); %Execute the query on the database and fetch the data DataPatients = fetch(NewConn, SQLquery); %Check if data are available for the given dates. If no data are %available then give an error and stop execution if (isempty(DataPatients)) errordlg('No patients were found within that date range') close(NewConn); % close the connection on error return end %Find on which study each patient is currently enrolled %Get the study descriptions from the STUDY table StudyIDs = fetch(NewConn, 'select STUDY_NUMBER from study'); DataStudies = cell(0); %define as empty cell array for Loop1 = 1:length(DataPatients) %For all patients find which studies they are enrolled from the %Study_Consent table SQLquery = ['select study_id from study_consent where patient_id ='... ,num2str(DataPatients{Loop1})]; %Execute the SQL statement and get the data TempDat = fetch(NewConn, SQLquery); %if patient is not enrolled in any studies yet then fill the field %with the default message if (isempty(TempDat)) DataStudies(Loop1) = {'Patient not enrolled in any studies'}; else %Fill in the first study as text
DataStudies(Loop1) = StudyIDs(TempDat{1}); %If patient is enrolled in more studies then append those to %the previous study separated by a comma if (length(TempDat) ~= 1) DataStudies(Loop1) = StudyIDs(TempDat{1}); for Loop2 = 2:length(TempDat) DataStudies(Loop1) = {[cell2mat(DataStudies(Loop1)),', '... cell2mat(StudyIDs(TempDat{Loop2}))]}; end end end end close(NewConn); %Close the connection if all is done %Write the excel file with the filename specified above xlswrite(ExcelFileName, [{'Patient number'},{'Studies enrolled'};... DataPatients, DataStudies']);