A1:笑得海潮 B3:冒泡的崔 D2:Cornell University,Computer Vision Group H2:冰河的博客 G3:丕子博客 K1:MLA CHINA K4:斯坦福视觉实验室 L4:MIT 机器学习实验室
现在的位置: 首页技术>正文
cat_ico23 category
matlab中连接操作MYSQL数据库
发表于863 天前 技术 暂无评论 ⁄ 被围观 1,072 次+

1.编写import_data.m子程序,实现从mysql读取数据到matlab中.
     1)下载mysql的jdbc的java驱动,安装到*:\Program Files\MATLAB71\java\jar\toolbox路径下;
      2)此“路径\文件名”添到*:\Program Files\MATLAB71\toolbox\local下的classpath.txt文件中;
     3)设置mysql下的数据库名称为带有“mysql ”后缀的,比如:abcmysql;
     4)服务器R的mysql下,输入“grant all on *.* to username@"L的IP" Identified by

         "password",允许L远程访问;
     5)复制如下代码到import_data文件中

          function MM=import_data();
             timeoutA=logintimeout(5)   
             % Set maximum time allowed for establishing a connection.
             connA = database('mysql', 'username', 'password','com.mysql.jdbc.Driver','jdbc:mysql:

            //R的IP:3306/abc')
             ping(connA)        % Check the database status.
             cursorA=exec(connA,'SELECT ALL CONLUMA,CONLUMB,CONLUMC FROM tablename);
            setdbprefs('DataReturnFormat','numeric');    %set numeric format
             cursorA=fetch(cursorA) ;     % Fetch all rows of data.
             MM=cursorA.Data;            % Display the data,save the data in MM
             close(cursorA) ;         % Close the cursor and the connection.
             close(connA)

       其中username,password,L\R的IP,数据库名称abc,tablename,sql语句都是要依实际修改的。

2.编写export_data.m子程序,实现从matlab写回数据到mysql中:
     原理类似, (tablename2的表要事先建好u)复制以下程序:

          timeoutA=logintimeout(5)   
             % Set maximum time allowed for establishing a connection.
             connA = database('mysql', 'username', 'password','com.mysql.jdbc.Driver','jdbc:mysql:

            //R的IP:3306/abc')
            ping(connA)        % Check the database status.
             %delete the records before updating
             cursorA=exec(connA,'delete tablename FROM tablename');
              Colnames = {'COLUMNA','COLUMNB','rCOLUMNC'};%updating
             fastinsert(connA, 'tablename2', Colnames,newdata);
             close(cursorA) ;  
              close(connA);

      其中username,password,R的IP,数据库名称abc,tablename,sql语句都是要以实际情况修改的,

     newdata就是suanfa.m程序得到结果组成的矩阵,这些结果将写到数据库中tablename2的表中。

给我留言


/ 快捷键:Ctrl+Enter

无觅相关文章插件,快速提升流量

不想听你唠叨×