qileilove

          blog已經轉移至github,大家請訪問 http://qaseven.github.io/

          MATLAB中關于MySQL數據庫的操作

          首先要安裝mysql驅動程序包,詳細步驟如下:
            Step 1:將mysql-connector-java-5.1.7-bin.jar文件拷貝到......\MATLAB\R2009a\java\jar\toolbox
            Step 2:到......\MATLAB\R2009a\toolbox\local目錄下,找到classpath.txt文件,打開,并添加用來加載mysql的jdbc驅動語句:
            $matlabroot/java/jar/toolbox/mysql-connector-java-5.1.7-bin.jar
            Step 3:重新打開MATLAB即可
            驅動程序安裝成功后,接來下要是matlab連接mysql數據庫的代碼:
            conn=database('databasename','username','password','driver','databaseurl')
            連接成功后,返回連接對象。
            參數如下:
            *databasename: 數據庫名稱.
            *driver: JDBC driver.
            *username and password: 用戶名和密碼.
            *databaseurl: 類似于jdbc:subprotocol:subname. subprotocol是數據庫類型,
            subname類似于//hostname:port/databasename.
            如果matlab和數據庫建立了連接,將返回類似于如下信息:
          Instance: 'SampleDB'
          UserName: ''
          Driver: []
          URL: []
          Constructor: [1x1com.mathworks.toolbox.database.databaseConnect]
          Message: []
          Handle: [1x1 sun.jdbc.odbc.JdbcOdbcConnection]
          TimeOut: 0
          AutoCommit: 'off'
          Type: 'Database Object'
            連接mysql的代碼如下:
            conn = database('tissueppi','root','root','com.mysql.jdbc.Driver','jdbc:mysql://localhost:3306/tissueppi');
            連接成功后,就可以用exec函數執行sql語句
            exec函數執行sql語句并返回一個開指針
            語法如下:
            curs =exec(conn,'sqlquery')
            例如:curs = exec(conn, 'select * fromcustomers')
            執行完查詢后,還要將查詢結果從開放cursor對象導入到對象curs中,該功能是用
            cursor.fetch函數實現的。

          語法如下:
            curs =fetch(curs)
            使用curs.Data來顯示數據,curs.Data返回一個CELL結構,可以先把CELL結構轉換成
            MATRIX結構再取值:
            cur=cell2mat(cur)
            a=cur(1,1);
            則查詢結果就加到了向量a中
            注意:
            在exec函數執行查詢過程中,有的sql語句要輸入變量,這時可使用strcat函數完成該
            功能。
          t =strcat(s1, s2, s3, ...)
          for(t=1:10)
          sql1 = strcat('select count(did) from rss_genepairs_u wheregocc>=',num2str(t),' || gomf>= ',num2str(t),' || gobp >=',num2str(t));
          end
            完整代碼如下:
          conn =database('tissueppi','root','root','com.mysql.jdbc.Driver','jdbc:mysql://localhost:3306/tissueppi');
          for t=0.5:0.01:0.91
          for x=0.5:0.1:11
          sql = strcat('select count(did) from rss_genepairs_x2 where score<=',num2str(x),' and did in(select did fromrss_genepairs_u where gocc >=',num2str(t),' || gomf>= ',num2str(t),' || gobp >=',num2str(t),')');
          aTemp = exec(conn,sql);
          aTemp = fetch(aTemp);
          a = aTemp.Data;
          a = cell2mat(a);
          a= a(1,1);
          end
          end

          posted on 2014-05-08 16:49 順其自然EVO 閱讀(2299) 評論(0)  編輯  收藏 所屬分類: 測試學習專欄數據庫

          <2014年5月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 房山区| 乌鲁木齐县| 沂水县| 林口县| 墨脱县| 济南市| 军事| 抚州市| 板桥市| 定结县| 三明市| 潼南县| 德钦县| 松阳县| 光山县| 盐池县| 金华市| 香格里拉县| 喀喇沁旗| 扶风县| 通河县| 明溪县| 多伦县| 门源| 巴南区| 邢台县| 伊吾县| 水城县| 焦作市| 开原市| 竹北市| 泰顺县| 阿合奇县| 平远县| 石景山区| 镇宁| 商都县| 新津县| 六枝特区| 文水县| 麻城市|