DBConnect - Connect to the database server. Open connection to MySQL database server. Connection information can be either passed explicitly as arguments, or read from your .my.cnf configuration file. USAGE DBConnect(host,user,password) host optional host (or host:port combination) user optional user password optional password NOTE Providing your login/password in the function call is not secure. Please consider storing this sensitive information in your .my.cnf file instead. SEE See also DBUse.
0001 function DBConnect(host,user,password) 0002 0003 %DBConnect - Connect to the database server. 0004 % 0005 % Open connection to MySQL database server. Connection information can 0006 % be either passed explicitly as arguments, or read from your .my.cnf 0007 % configuration file. 0008 % 0009 % USAGE 0010 % 0011 % DBConnect(host,user,password) 0012 % 0013 % host optional host (or host:port combination) 0014 % user optional user 0015 % password optional password 0016 % 0017 % NOTE 0018 % 0019 % Providing your login/password in the function call is not secure. 0020 % Please consider storing this sensitive information in your .my.cnf 0021 % file instead. 0022 % 0023 % SEE 0024 % 0025 % See also DBUse. 0026 % 0027 0028 % Copyright (C) 2007-2016 by Michaƫl Zugaro 0029 % 0030 % This program is free software; you can redistribute it and/or modify 0031 % it under the terms of the GNU General Public License as published by 0032 % the Free Software Foundation; either version 3 of the License, or 0033 % (at your option) any later version. 0034 0035 global dbUser; 0036 0037 % Make sure MyM is installed and functional 0038 CheckMyM; 0039 0040 % Try reading account info from config file 0041 cfHost = ''; 0042 cfUser = ''; 0043 cfPassword = ''; 0044 cfPort = '3306'; 0045 file = fopen('~/.my.cnf'); 0046 if file ~= -1, 0047 while true, 0048 line = fgetl(file); 0049 if isempty(line) | line == -1, break; end 0050 equal = findstr('=',line); 0051 if isempty(equal), continue; end 0052 switch line(1:equal-1), 0053 case 'host', 0054 cfHost = line(equal+1:end); 0055 case 'port', 0056 cfPort = line(equal+1:end); 0057 case 'user', 0058 cfUser = line(equal+1:end); 0059 case 'password', 0060 cfPassword = line(equal+1:end); 0061 end 0062 end 0063 fclose(file); 0064 end 0065 0066 % Parse parameter list 0067 if nargin < 3, 0068 password = cfPassword; 0069 else 0070 warning('FMAToolbox:DBConnect:InsecureLogin','Providing your login/password in the function call is not secure.\nPlease consider storing this sensitive information in your .my.cnf file instead') 0071 end 0072 if nargin < 2, 0073 user = cfUser; 0074 end 0075 if nargin < 1, 0076 host = cfHost; 0077 end 0078 0079 port = cfPort; 0080 dbUser = user; 0081 0082 try 0083 h = mym('open',[host ':' port],user,password); 0084 catch e 0085 error(['Could not connect to MySQL. Check your login/password (' e.message ').']); 0086 end