-2

我真的不喜欢 Matlab 的 GUI 和 MySQL 连接器,所以我尝试使用 Python 来代替。我已将 .m 文件编译为 dll(mcc -l filname.m),但我不知道如何调用函数并传递参数。请帮助我。

4

1 回答 1

0

本教程解释了如何在 Matlab 中使用 MySQL。请注意,您需要在此处下载 MySQL 连接器/J JAR (类似于mysql-connector-java-5.1.25-bin.jar)。

http://www.stanford.edu/group/farmshare/cgi-bin/wiki/index.php/MatlabMysql

% Database Server
host = 'mysql-user.stanford.edu';

% Database Username/Password
user = 'gfarmsharetest';
password = 'putyourpasswordhere';

% Database Name
dbName = 'g_farmshare_testing';

% JDBC Parameters
jdbcString = sprintf('jdbc:mysql://%s/%s', host, dbName);
jdbcDriver = 'com.mysql.jdbc.Driver';

% Set this to the path to your MySQL Connector/J JAR
javaaddpath('/usr/share/java/mysql-connector-java.jar')
% On Windows it looks like: javaaddpath('C:\server\mysql-connector-java-5.1.25\mysql-connector-java-5.1.25-bin.jar')


% Create the database connection object
dbConn = database(dbName, user , password, jdbcDriver, jdbcString);

% Check to make sure that we successfully connected
if isconnection(dbConn)
    % Fetch the symbol, market cap, and last close for the 10 largest
    % market cap ETFs
    result = get(fetch(exec(dbConn, 'SELECT foo,bar from matlabfoo')), 'Data');
    disp(result);
else
    % If the connection failed, print the error message
    disp(sprintf('Connection failed: %s', dbConn.Message));
end

要运行查询:

%% Running queries
% http://www.mathworks.com/help/database/run-sql-query.html

cursor = exec(connection, 'SELECT user_id FROM moocdb.users LIMIT 1000')
a = fetch(cursor)
a.Data

sql = [' SELECT observed_events.observed_event_duration ' ... 
    ' FROM moocdb.observed_events AS observed_events ' ...
    ' LIMIT 1000000000; ']
boxplot(cell2mat(a.Data))

cursor = exec(connection,sql);
a = fetch(cursor)
boxplot(cell2mat(a.Data))

% Close the connection so we don't run out of MySQL threads
close(connection);
于 2013-10-03T05:23:59.127 回答