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 閱讀(2301) 評論(0)  編輯  收藏 所屬分類: 測試學習專欄數據庫

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

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 开封市| 宣武区| 南开区| 田东县| 邯郸市| 天峨县| 东乌珠穆沁旗| 濮阳县| 东海县| 金坛市| 搜索| 克山县| 临沭县| 青浦区| 繁峙县| 光山县| 元阳县| 永寿县| 霸州市| 集贤县| 定兴县| 琼结县| 榆林市| 师宗县| 云阳县| 夏邑县| 上饶市| 东明县| 体育| 永平县| 松潘县| 定襄县| 平武县| 塔河县| 庄浪县| 黄山市| 旺苍县| 彝良县| 台江县| 白山市| 红安县|