RMySQL读取mysql数据库的尝试

刚刚很艰难的在windows下安装上RMySQL,详见

RMySQL在windows下的安装方法

迫不及待的测试了一下

参考资料:http://www.biosino.org/R/R-doc/R-data_cn/DBI-_002f-RMySQL.html

 

1、连接数据库

> library(RMySQL) #加载包
> con <- dbConnect(dbDriver("MySQL"), dbname = "eswp", user="root", password="root")
  #连接数据库,必须通过user和password输入用户名和密码,不然连不上

2、列出表中数据库

 dbListTables(con)
 [1] "2008yearnew"                  "agent"                        "artifact"                    
 [4] "cancer_data"                  "chinese_drug"                 "conceptnum"  

3、读取table

> dbReadTable(con, "foreign_drug")[3:7,1:2]#数据太多,只读取一部分
                                                                                                        URL register_no
3 http://app1.sfda.gov.cn/datasearch/face3/content.jsp?tableId=36&tableName=TABLE36&tableView=????&Id=10987   H20120197
4 http://app1.sfda.gov.cn/datasearch/face3/content.jsp?tableId=36&tableName=TABLE36&tableView=????&Id=10990   H20120196
5 http://app1.sfda.gov.cn/datasearch/face3/content.jsp?tableId=36&tableName=TABLE36&tableView=????&Id=10984   H20120181
6 http://app1.sfda.gov.cn/datasearch/face3/content.jsp?tableId=36&tableName=TABLE36&tableView=????&Id=10993   H20120184
7 http://app1.sfda.gov.cn/datasearch/face3/content.jsp?tableId=36&tableName=TABLE36&tableView=????&Id=10978   H20120182

4、查询table

> dbGetQuery(con, paste("select URL,register_no from foreign_drug where register_no ='H20120197' or register_no ='H20120196'"))
                                                                                                        URL register_no
1 http://app1.sfda.gov.cn/datasearch/face3/content.jsp?tableId=36&tableName=TABLE36&tableView=????&Id=10987   H20120197
2 http://app1.sfda.gov.cn/datasearch/face3/content.jsp?tableId=36&tableName=TABLE36&tableView=????&Id=10990   H20120196

5、删除表,断开连接

> dbRemoveTable(con, "foreign_drug")
[1] TRUE
> dbDisconnect(con)
[1] TRUE

 

先回去了,明天接着学习。

生命不息,学习不止。

posted @ 2012-07-17 20:04  todoit  阅读(997)  评论(0编辑  收藏  举报