声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 2918|回复: 3

[编程技巧] matlab如何从mysql中读取数据

[复制链接]
发表于 2015-10-6 09:12 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
matlab如何从mysql中读取数据
回复
分享到:

使用道具 举报

发表于 2015-10-8 10:53 | 显示全部楼层
Windows XP系统下,Matlab访问MySQL的方法已经有多伦多大学的Robert Almgren (http://www.courant.nyu.edu/~almgren/mysql/ )开发出的很简单有效的方法。

背景熟悉:
JDBC驱动的详细使用( 太长,只在里面搜索 Driver 和 URL 看看怎么添写):  http://www.developer.com/java/da ... Getting-Started.htm
Matlab 连接Mysql时的驱动编写方法:
http://www.mathworks.cn/help/toolbox/database/ug/database.html

一、驱动程序的配置
这部分内容完全参考《matlab的database toolbox通过 jdbc连接mysql配置》[1] 。
http://dev.mysql.com/downloads/connector/j/  下载MySQL的jdbc压缩包,只用到其中的 mysql-connector-java-5.1.13-bin.jar,将此jar包复制至matlab的java tool文件夹下。然后把这个路径(要包含文件)添加 到 $matlabroot/toolbox/local/classpath.txt下. 其中 $matlabroot 指的是 matlab 安装的主目录(我的是 /home/用户名/matlab)
实例:
I)下载得到mysql-connector-java-5.1.13-bin.jar;
II)复制至D:\Program Files\Matlab\R2009b\java\jar 文件夹下;
III)对路径D:\Program Files\Matlab\R2009b\toolbox\local 下得classpath.txt 文档,增加了一句话,用来加载mysql的jdbc驱动$matlabroot/java/jar/toolbox/mysql-connector-java-5.1.7-bin.jar, 关闭matlab,重新打开,一切OK。
二、使用示例
这部分内容参考《matlab连接mysql》[2]
安装完jdbc驱动后,重新打开matlab,然后输入
conn = database(‘MovieScript’,'root’,'pwd’,'com.mysql.jdbc.Driver’,'jdbc:mysql://127.0.0.1:3306/MovieScript’);
在matlab的官方Document中,我们可以知道上面命令的各个参数意义分别是:
第一个参数:数据库的名称,就是作为操作目标的数据库的名称
第二个参数:用户名
第三个参数:密码
第四个参数:连接的驱动,这里就写这个,不用改
第五个参数:数据库的连接路径吧,jdbc:mysql://,前面这个是jdbc,用mysql数据库,后边是具体的路径,数据库的IP,端口,和数据库的名称,跟第一个参数一样
ping(conn) 一下,就可以知道是否连接成功了
然后测试
cursor = exec(conn,’SELECT speaker, groundtruth_id FROM Friends_02_02′); % 执行sql查询
cursorA =fetch(cursor) ; % 取数据结果
cursorA.data % 就是最终的数据,是一个CELL结构
使用完数据库后,依次关闭游标和连接
close(curs);
close(conn);
三、参考文章及连接
[1] Matlab Database Toolbox: http://www.mathworks.cn/help/toolbox/database/ug/database.html
[2] 《maylab连接mysql》: http://blog.csdn.net/wangwei200508/article/details/5883140
[3] Exporting Data from MATLAB to a New Record in a Database:  http://poli.feld.cvut.cz/ucebna/ ... abase/tutor-34.html
[4] Importing Data from MATLAB to a Database:
http://www.ee.kth.se/~phoebus/330NEST/config_files/importDB.m


另外补一下Matlab本身提供的querybuilder命令
输入 querybuilder, 在弹出的窗口中 单击 Query > Define JDBC Data Source.  单击 Create New File... 输入 Name,Driver,URL
我的是:
Name: test
Driver: com.mysql.jdbc.Driver
URL: jdbc:mysql://localhost:3306/
其中test 是mysql 中一个 database的名字, 3306 是 mysql 的端口号
然后 单击 Add/Update 按钮,OK, 回到 Visual Query Builder, 单击 Data source 下的 test条目,按要求输入mysql的用户名和密码,然后就可以看到 Tables 里面显示出 表名了。

以上内容来自:http://blog.sina.com.cn/s/blog_5071eb880100w1i3.html
发表于 2015-10-8 10:54 | 显示全部楼层
首先要安装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: [1x1 com.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 * from customers')
执行完查询后,还要将查询结果从开放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 where gocc>=',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 from rss_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

以上内容来自:http://blog.sina.com.cn/s/blog_9d0b00a401012spy.html
 楼主| 发表于 2015-10-8 22:56 | 显示全部楼层
happy 发表于 2015-10-8 10:54
首先要安装mysql驱动程序包,详细步骤如下:
Step 1: 将mysql-connector-java-5.1.7-bin.jar文件拷贝到... ...

谢谢,先学习一下
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-5-18 08:18 , Processed in 0.088671 second(s), 21 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表